mce.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345
  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/ratelimit.h>
  14. #include <linux/kallsyms.h>
  15. #include <linux/rcupdate.h>
  16. #include <linux/smp_lock.h>
  17. #include <linux/kobject.h>
  18. #include <linux/kdebug.h>
  19. #include <linux/kernel.h>
  20. #include <linux/percpu.h>
  21. #include <linux/string.h>
  22. #include <linux/sysdev.h>
  23. #include <linux/ctype.h>
  24. #include <linux/sched.h>
  25. #include <linux/sysfs.h>
  26. #include <linux/types.h>
  27. #include <linux/init.h>
  28. #include <linux/kmod.h>
  29. #include <linux/poll.h>
  30. #include <linux/cpu.h>
  31. #include <linux/fs.h>
  32. #include <asm/processor.h>
  33. #include <asm/uaccess.h>
  34. #include <asm/idle.h>
  35. #include <asm/mce.h>
  36. #include <asm/msr.h>
  37. #include <asm/smp.h>
  38. #include "mce.h"
  39. /* Handle unconfigured int18 (should never happen) */
  40. static void unexpected_machine_check(struct pt_regs *regs, long error_code)
  41. {
  42. printk(KERN_ERR "CPU#%d: Unexpected int18 (Machine Check).\n",
  43. smp_processor_id());
  44. }
  45. /* Call the installed machine check handler for this CPU setup. */
  46. void (*machine_check_vector)(struct pt_regs *, long error_code) =
  47. unexpected_machine_check;
  48. int mce_disabled;
  49. #ifdef CONFIG_X86_64
  50. #define MISC_MCELOG_MINOR 227
  51. atomic_t mce_entry;
  52. /*
  53. * Tolerant levels:
  54. * 0: always panic on uncorrected errors, log corrected errors
  55. * 1: panic or SIGBUS on uncorrected errors, log corrected errors
  56. * 2: SIGBUS or log uncorrected errors (if possible), log corrected errors
  57. * 3: never panic or SIGBUS, log all errors (for testing only)
  58. */
  59. static int tolerant = 1;
  60. static int banks;
  61. static u64 *bank;
  62. static unsigned long notify_user;
  63. static int rip_msr;
  64. static int mce_bootlog = -1;
  65. static atomic_t mce_events;
  66. static char trigger[128];
  67. static char *trigger_argv[2] = { trigger, NULL };
  68. static unsigned long dont_init_banks;
  69. static DECLARE_WAIT_QUEUE_HEAD(mce_wait);
  70. /* MCA banks polled by the period polling timer for corrected events */
  71. DEFINE_PER_CPU(mce_banks_t, mce_poll_banks) = {
  72. [0 ... BITS_TO_LONGS(MAX_NR_BANKS)-1] = ~0UL
  73. };
  74. static inline int skip_bank_init(int i)
  75. {
  76. return i < BITS_PER_LONG && test_bit(i, &dont_init_banks);
  77. }
  78. /* Do initial initialization of a struct mce */
  79. void mce_setup(struct mce *m)
  80. {
  81. memset(m, 0, sizeof(struct mce));
  82. m->cpu = smp_processor_id();
  83. rdtscll(m->tsc);
  84. }
  85. /*
  86. * Lockless MCE logging infrastructure.
  87. * This avoids deadlocks on printk locks without having to break locks. Also
  88. * separate MCEs from kernel messages to avoid bogus bug reports.
  89. */
  90. static struct mce_log mcelog = {
  91. MCE_LOG_SIGNATURE,
  92. MCE_LOG_LEN,
  93. };
  94. void mce_log(struct mce *mce)
  95. {
  96. unsigned next, entry;
  97. atomic_inc(&mce_events);
  98. mce->finished = 0;
  99. wmb();
  100. for (;;) {
  101. entry = rcu_dereference(mcelog.next);
  102. for (;;) {
  103. /*
  104. * When the buffer fills up discard new entries.
  105. * Assume that the earlier errors are the more
  106. * interesting ones:
  107. */
  108. if (entry >= MCE_LOG_LEN) {
  109. set_bit(MCE_OVERFLOW, (unsigned long *)&mcelog.flags);
  110. return;
  111. }
  112. /* Old left over entry. Skip: */
  113. if (mcelog.entry[entry].finished) {
  114. entry++;
  115. continue;
  116. }
  117. break;
  118. }
  119. smp_rmb();
  120. next = entry + 1;
  121. if (cmpxchg(&mcelog.next, entry, next) == entry)
  122. break;
  123. }
  124. memcpy(mcelog.entry + entry, mce, sizeof(struct mce));
  125. wmb();
  126. mcelog.entry[entry].finished = 1;
  127. wmb();
  128. set_bit(0, &notify_user);
  129. }
  130. static void print_mce(struct mce *m)
  131. {
  132. printk(KERN_EMERG "\n"
  133. KERN_EMERG "HARDWARE ERROR\n"
  134. KERN_EMERG
  135. "CPU %d: Machine Check Exception: %16Lx Bank %d: %016Lx\n",
  136. m->cpu, m->mcgstatus, m->bank, m->status);
  137. if (m->ip) {
  138. printk(KERN_EMERG "RIP%s %02x:<%016Lx> ",
  139. !(m->mcgstatus & MCG_STATUS_EIPV) ? " !INEXACT!" : "",
  140. m->cs, m->ip);
  141. if (m->cs == __KERNEL_CS)
  142. print_symbol("{%s}", m->ip);
  143. printk("\n");
  144. }
  145. printk(KERN_EMERG "TSC %llx ", m->tsc);
  146. if (m->addr)
  147. printk("ADDR %llx ", m->addr);
  148. if (m->misc)
  149. printk("MISC %llx ", m->misc);
  150. printk("\n");
  151. printk(KERN_EMERG "This is not a software problem!\n");
  152. printk(KERN_EMERG "Run through mcelog --ascii to decode "
  153. "and contact your hardware vendor\n");
  154. }
  155. static void mce_panic(char *msg, struct mce *backup, u64 start)
  156. {
  157. int i;
  158. oops_begin();
  159. for (i = 0; i < MCE_LOG_LEN; i++) {
  160. u64 tsc = mcelog.entry[i].tsc;
  161. if ((s64)(tsc - start) < 0)
  162. continue;
  163. print_mce(&mcelog.entry[i]);
  164. if (backup && mcelog.entry[i].tsc == backup->tsc)
  165. backup = NULL;
  166. }
  167. if (backup)
  168. print_mce(backup);
  169. panic(msg);
  170. }
  171. int mce_available(struct cpuinfo_x86 *c)
  172. {
  173. if (mce_disabled)
  174. return 0;
  175. return cpu_has(c, X86_FEATURE_MCE) && cpu_has(c, X86_FEATURE_MCA);
  176. }
  177. static inline void mce_get_rip(struct mce *m, struct pt_regs *regs)
  178. {
  179. if (regs && (m->mcgstatus & MCG_STATUS_RIPV)) {
  180. m->ip = regs->ip;
  181. m->cs = regs->cs;
  182. } else {
  183. m->ip = 0;
  184. m->cs = 0;
  185. }
  186. if (rip_msr) {
  187. /* Assume the RIP in the MSR is exact. Is this true? */
  188. m->mcgstatus |= MCG_STATUS_EIPV;
  189. rdmsrl(rip_msr, m->ip);
  190. m->cs = 0;
  191. }
  192. }
  193. /*
  194. * Poll for corrected events or events that happened before reset.
  195. * Those are just logged through /dev/mcelog.
  196. *
  197. * This is executed in standard interrupt context.
  198. */
  199. void machine_check_poll(enum mcp_flags flags, mce_banks_t *b)
  200. {
  201. struct mce m;
  202. int i;
  203. mce_setup(&m);
  204. rdmsrl(MSR_IA32_MCG_STATUS, m.mcgstatus);
  205. for (i = 0; i < banks; i++) {
  206. if (!bank[i] || !test_bit(i, *b))
  207. continue;
  208. m.misc = 0;
  209. m.addr = 0;
  210. m.bank = i;
  211. m.tsc = 0;
  212. barrier();
  213. rdmsrl(MSR_IA32_MC0_STATUS + i*4, m.status);
  214. if (!(m.status & MCI_STATUS_VAL))
  215. continue;
  216. /*
  217. * Uncorrected events are handled by the exception handler
  218. * when it is enabled. But when the exception is disabled log
  219. * everything.
  220. *
  221. * TBD do the same check for MCI_STATUS_EN here?
  222. */
  223. if ((m.status & MCI_STATUS_UC) && !(flags & MCP_UC))
  224. continue;
  225. if (m.status & MCI_STATUS_MISCV)
  226. rdmsrl(MSR_IA32_MC0_MISC + i*4, m.misc);
  227. if (m.status & MCI_STATUS_ADDRV)
  228. rdmsrl(MSR_IA32_MC0_ADDR + i*4, m.addr);
  229. if (!(flags & MCP_TIMESTAMP))
  230. m.tsc = 0;
  231. /*
  232. * Don't get the IP here because it's unlikely to
  233. * have anything to do with the actual error location.
  234. */
  235. if (!(flags & MCP_DONTLOG)) {
  236. mce_log(&m);
  237. add_taint(TAINT_MACHINE_CHECK);
  238. }
  239. /*
  240. * Clear state for this bank.
  241. */
  242. wrmsrl(MSR_IA32_MC0_STATUS+4*i, 0);
  243. }
  244. /*
  245. * Don't clear MCG_STATUS here because it's only defined for
  246. * exceptions.
  247. */
  248. }
  249. /*
  250. * The actual machine check handler. This only handles real
  251. * exceptions when something got corrupted coming in through int 18.
  252. *
  253. * This is executed in NMI context not subject to normal locking rules. This
  254. * implies that most kernel services cannot be safely used. Don't even
  255. * think about putting a printk in there!
  256. */
  257. void do_machine_check(struct pt_regs *regs, long error_code)
  258. {
  259. struct mce m, panicm;
  260. int panicm_found = 0;
  261. u64 mcestart = 0;
  262. int i;
  263. /*
  264. * If no_way_out gets set, there is no safe way to recover from this
  265. * MCE. If tolerant is cranked up, we'll try anyway.
  266. */
  267. int no_way_out = 0;
  268. /*
  269. * If kill_it gets set, there might be a way to recover from this
  270. * error.
  271. */
  272. int kill_it = 0;
  273. DECLARE_BITMAP(toclear, MAX_NR_BANKS);
  274. atomic_inc(&mce_entry);
  275. if (notify_die(DIE_NMI, "machine check", regs, error_code,
  276. 18, SIGKILL) == NOTIFY_STOP)
  277. goto out2;
  278. if (!banks)
  279. goto out2;
  280. mce_setup(&m);
  281. rdmsrl(MSR_IA32_MCG_STATUS, m.mcgstatus);
  282. /* if the restart IP is not valid, we're done for */
  283. if (!(m.mcgstatus & MCG_STATUS_RIPV))
  284. no_way_out = 1;
  285. rdtscll(mcestart);
  286. barrier();
  287. for (i = 0; i < banks; i++) {
  288. __clear_bit(i, toclear);
  289. if (!bank[i])
  290. continue;
  291. m.misc = 0;
  292. m.addr = 0;
  293. m.bank = i;
  294. rdmsrl(MSR_IA32_MC0_STATUS + i*4, m.status);
  295. if ((m.status & MCI_STATUS_VAL) == 0)
  296. continue;
  297. /*
  298. * Non uncorrected errors are handled by machine_check_poll
  299. * Leave them alone.
  300. */
  301. if ((m.status & MCI_STATUS_UC) == 0)
  302. continue;
  303. /*
  304. * Set taint even when machine check was not enabled.
  305. */
  306. add_taint(TAINT_MACHINE_CHECK);
  307. __set_bit(i, toclear);
  308. if (m.status & MCI_STATUS_EN) {
  309. /* if PCC was set, there's no way out */
  310. no_way_out |= !!(m.status & MCI_STATUS_PCC);
  311. /*
  312. * If this error was uncorrectable and there was
  313. * an overflow, we're in trouble. If no overflow,
  314. * we might get away with just killing a task.
  315. */
  316. if (m.status & MCI_STATUS_UC) {
  317. if (tolerant < 1 || m.status & MCI_STATUS_OVER)
  318. no_way_out = 1;
  319. kill_it = 1;
  320. }
  321. } else {
  322. /*
  323. * Machine check event was not enabled. Clear, but
  324. * ignore.
  325. */
  326. continue;
  327. }
  328. if (m.status & MCI_STATUS_MISCV)
  329. rdmsrl(MSR_IA32_MC0_MISC + i*4, m.misc);
  330. if (m.status & MCI_STATUS_ADDRV)
  331. rdmsrl(MSR_IA32_MC0_ADDR + i*4, m.addr);
  332. mce_get_rip(&m, regs);
  333. mce_log(&m);
  334. /*
  335. * Did this bank cause the exception?
  336. *
  337. * Assume that the bank with uncorrectable errors did it,
  338. * and that there is only a single one:
  339. */
  340. if ((m.status & MCI_STATUS_UC) &&
  341. (m.status & MCI_STATUS_EN)) {
  342. panicm = m;
  343. panicm_found = 1;
  344. }
  345. }
  346. /*
  347. * If we didn't find an uncorrectable error, pick
  348. * the last one (shouldn't happen, just being safe).
  349. */
  350. if (!panicm_found)
  351. panicm = m;
  352. /*
  353. * If we have decided that we just CAN'T continue, and the user
  354. * has not set tolerant to an insane level, give up and die.
  355. */
  356. if (no_way_out && tolerant < 3)
  357. mce_panic("Machine check", &panicm, mcestart);
  358. /*
  359. * If the error seems to be unrecoverable, something should be
  360. * done. Try to kill as little as possible. If we can kill just
  361. * one task, do that. If the user has set the tolerance very
  362. * high, don't try to do anything at all.
  363. */
  364. if (kill_it && tolerant < 3) {
  365. int user_space = 0;
  366. /*
  367. * If the EIPV bit is set, it means the saved IP is the
  368. * instruction which caused the MCE.
  369. */
  370. if (m.mcgstatus & MCG_STATUS_EIPV)
  371. user_space = panicm.ip && (panicm.cs & 3);
  372. /*
  373. * If we know that the error was in user space, send a
  374. * SIGBUS. Otherwise, panic if tolerance is low.
  375. *
  376. * force_sig() takes an awful lot of locks and has a slight
  377. * risk of deadlocking.
  378. */
  379. if (user_space) {
  380. force_sig(SIGBUS, current);
  381. } else if (panic_on_oops || tolerant < 2) {
  382. mce_panic("Uncorrected machine check",
  383. &panicm, mcestart);
  384. }
  385. }
  386. /* notify userspace ASAP */
  387. set_thread_flag(TIF_MCE_NOTIFY);
  388. /* the last thing we do is clear state */
  389. for (i = 0; i < banks; i++) {
  390. if (test_bit(i, toclear))
  391. wrmsrl(MSR_IA32_MC0_STATUS+4*i, 0);
  392. }
  393. wrmsrl(MSR_IA32_MCG_STATUS, 0);
  394. out2:
  395. atomic_dec(&mce_entry);
  396. }
  397. #ifdef CONFIG_X86_MCE_INTEL
  398. /***
  399. * mce_log_therm_throt_event - Logs the thermal throttling event to mcelog
  400. * @cpu: The CPU on which the event occurred.
  401. * @status: Event status information
  402. *
  403. * This function should be called by the thermal interrupt after the
  404. * event has been processed and the decision was made to log the event
  405. * further.
  406. *
  407. * The status parameter will be saved to the 'status' field of 'struct mce'
  408. * and historically has been the register value of the
  409. * MSR_IA32_THERMAL_STATUS (Intel) msr.
  410. */
  411. void mce_log_therm_throt_event(__u64 status)
  412. {
  413. struct mce m;
  414. mce_setup(&m);
  415. m.bank = MCE_THERMAL_BANK;
  416. m.status = status;
  417. mce_log(&m);
  418. }
  419. #endif /* CONFIG_X86_MCE_INTEL */
  420. /*
  421. * Periodic polling timer for "silent" machine check errors. If the
  422. * poller finds an MCE, poll 2x faster. When the poller finds no more
  423. * errors, poll 2x slower (up to check_interval seconds).
  424. */
  425. static int check_interval = 5 * 60; /* 5 minutes */
  426. static DEFINE_PER_CPU(int, next_interval); /* in jiffies */
  427. static DEFINE_PER_CPU(struct timer_list, mce_timer);
  428. static void mcheck_timer(unsigned long data)
  429. {
  430. struct timer_list *t = &per_cpu(mce_timer, data);
  431. int *n;
  432. WARN_ON(smp_processor_id() != data);
  433. if (mce_available(&current_cpu_data)) {
  434. machine_check_poll(MCP_TIMESTAMP,
  435. &__get_cpu_var(mce_poll_banks));
  436. }
  437. /*
  438. * Alert userspace if needed. If we logged an MCE, reduce the
  439. * polling interval, otherwise increase the polling interval.
  440. */
  441. n = &__get_cpu_var(next_interval);
  442. if (mce_notify_user()) {
  443. *n = max(*n/2, HZ/100);
  444. } else {
  445. *n = min(*n*2, (int)round_jiffies_relative(check_interval*HZ));
  446. }
  447. t->expires = jiffies + *n;
  448. add_timer(t);
  449. }
  450. static void mce_do_trigger(struct work_struct *work)
  451. {
  452. call_usermodehelper(trigger, trigger_argv, NULL, UMH_NO_WAIT);
  453. }
  454. static DECLARE_WORK(mce_trigger_work, mce_do_trigger);
  455. /*
  456. * Notify the user(s) about new machine check events.
  457. * Can be called from interrupt context, but not from machine check/NMI
  458. * context.
  459. */
  460. int mce_notify_user(void)
  461. {
  462. /* Not more than two messages every minute */
  463. static DEFINE_RATELIMIT_STATE(ratelimit, 60*HZ, 2);
  464. clear_thread_flag(TIF_MCE_NOTIFY);
  465. if (test_and_clear_bit(0, &notify_user)) {
  466. wake_up_interruptible(&mce_wait);
  467. /*
  468. * There is no risk of missing notifications because
  469. * work_pending is always cleared before the function is
  470. * executed.
  471. */
  472. if (trigger[0] && !work_pending(&mce_trigger_work))
  473. schedule_work(&mce_trigger_work);
  474. if (__ratelimit(&ratelimit))
  475. printk(KERN_INFO "Machine check events logged\n");
  476. return 1;
  477. }
  478. return 0;
  479. }
  480. /* see if the idle task needs to notify userspace: */
  481. static int
  482. mce_idle_callback(struct notifier_block *nfb, unsigned long action,
  483. void *unused)
  484. {
  485. /* IDLE_END should be safe - interrupts are back on */
  486. if (action == IDLE_END && test_thread_flag(TIF_MCE_NOTIFY))
  487. mce_notify_user();
  488. return NOTIFY_OK;
  489. }
  490. static struct notifier_block mce_idle_notifier = {
  491. .notifier_call = mce_idle_callback,
  492. };
  493. static __init int periodic_mcheck_init(void)
  494. {
  495. idle_notifier_register(&mce_idle_notifier);
  496. return 0;
  497. }
  498. __initcall(periodic_mcheck_init);
  499. /*
  500. * Initialize Machine Checks for a CPU.
  501. */
  502. static int mce_cap_init(void)
  503. {
  504. unsigned b;
  505. u64 cap;
  506. rdmsrl(MSR_IA32_MCG_CAP, cap);
  507. b = cap & MCG_BANKCNT_MASK;
  508. printk(KERN_INFO "mce: CPU supports %d MCE banks\n", b);
  509. if (b > MAX_NR_BANKS) {
  510. printk(KERN_WARNING
  511. "MCE: Using only %u machine check banks out of %u\n",
  512. MAX_NR_BANKS, b);
  513. b = MAX_NR_BANKS;
  514. }
  515. /* Don't support asymmetric configurations today */
  516. WARN_ON(banks != 0 && b != banks);
  517. banks = b;
  518. if (!bank) {
  519. bank = kmalloc(banks * sizeof(u64), GFP_KERNEL);
  520. if (!bank)
  521. return -ENOMEM;
  522. memset(bank, 0xff, banks * sizeof(u64));
  523. }
  524. /* Use accurate RIP reporting if available. */
  525. if ((cap & MCG_EXT_P) && MCG_EXT_CNT(cap) >= 9)
  526. rip_msr = MSR_IA32_MCG_EIP;
  527. return 0;
  528. }
  529. static void mce_init(void *dummy)
  530. {
  531. mce_banks_t all_banks;
  532. u64 cap;
  533. int i;
  534. /*
  535. * Log the machine checks left over from the previous reset.
  536. */
  537. bitmap_fill(all_banks, MAX_NR_BANKS);
  538. machine_check_poll(MCP_UC|(!mce_bootlog ? MCP_DONTLOG : 0), &all_banks);
  539. set_in_cr4(X86_CR4_MCE);
  540. rdmsrl(MSR_IA32_MCG_CAP, cap);
  541. if (cap & MCG_CTL_P)
  542. wrmsr(MSR_IA32_MCG_CTL, 0xffffffff, 0xffffffff);
  543. for (i = 0; i < banks; i++) {
  544. if (skip_bank_init(i))
  545. continue;
  546. wrmsrl(MSR_IA32_MC0_CTL+4*i, bank[i]);
  547. wrmsrl(MSR_IA32_MC0_STATUS+4*i, 0);
  548. }
  549. }
  550. /* Add per CPU specific workarounds here */
  551. static void mce_cpu_quirks(struct cpuinfo_x86 *c)
  552. {
  553. /* This should be disabled by the BIOS, but isn't always */
  554. if (c->x86_vendor == X86_VENDOR_AMD) {
  555. if (c->x86 == 15 && banks > 4) {
  556. /*
  557. * disable GART TBL walk error reporting, which
  558. * trips off incorrectly with the IOMMU & 3ware
  559. * & Cerberus:
  560. */
  561. clear_bit(10, (unsigned long *)&bank[4]);
  562. }
  563. if (c->x86 <= 17 && mce_bootlog < 0) {
  564. /*
  565. * Lots of broken BIOS around that don't clear them
  566. * by default and leave crap in there. Don't log:
  567. */
  568. mce_bootlog = 0;
  569. }
  570. /*
  571. * Various K7s with broken bank 0 around. Always disable
  572. * by default.
  573. */
  574. if (c->x86 == 6)
  575. bank[0] = 0;
  576. }
  577. if (c->x86_vendor == X86_VENDOR_INTEL) {
  578. /*
  579. * SDM documents that on family 6 bank 0 should not be written
  580. * because it aliases to another special BIOS controlled
  581. * register.
  582. * But it's not aliased anymore on model 0x1a+
  583. * Don't ignore bank 0 completely because there could be a
  584. * valid event later, merely don't write CTL0.
  585. */
  586. if (c->x86 == 6 && c->x86_model < 0x1A)
  587. __set_bit(0, &dont_init_banks);
  588. }
  589. }
  590. static void mce_cpu_features(struct cpuinfo_x86 *c)
  591. {
  592. switch (c->x86_vendor) {
  593. case X86_VENDOR_INTEL:
  594. mce_intel_feature_init(c);
  595. break;
  596. case X86_VENDOR_AMD:
  597. mce_amd_feature_init(c);
  598. break;
  599. default:
  600. break;
  601. }
  602. }
  603. static void mce_init_timer(void)
  604. {
  605. struct timer_list *t = &__get_cpu_var(mce_timer);
  606. int *n = &__get_cpu_var(next_interval);
  607. *n = check_interval * HZ;
  608. if (!*n)
  609. return;
  610. setup_timer(t, mcheck_timer, smp_processor_id());
  611. t->expires = round_jiffies(jiffies + *n);
  612. add_timer(t);
  613. }
  614. /*
  615. * Called for each booted CPU to set up machine checks.
  616. * Must be called with preempt off:
  617. */
  618. void __cpuinit mcheck_init(struct cpuinfo_x86 *c)
  619. {
  620. if (!mce_available(c))
  621. return;
  622. if (mce_cap_init() < 0) {
  623. mce_disabled = 1;
  624. return;
  625. }
  626. mce_cpu_quirks(c);
  627. machine_check_vector = do_machine_check;
  628. mce_init(NULL);
  629. mce_cpu_features(c);
  630. mce_init_timer();
  631. }
  632. /*
  633. * Character device to read and clear the MCE log.
  634. */
  635. static DEFINE_SPINLOCK(mce_state_lock);
  636. static int open_count; /* #times opened */
  637. static int open_exclu; /* already open exclusive? */
  638. static int mce_open(struct inode *inode, struct file *file)
  639. {
  640. lock_kernel();
  641. spin_lock(&mce_state_lock);
  642. if (open_exclu || (open_count && (file->f_flags & O_EXCL))) {
  643. spin_unlock(&mce_state_lock);
  644. unlock_kernel();
  645. return -EBUSY;
  646. }
  647. if (file->f_flags & O_EXCL)
  648. open_exclu = 1;
  649. open_count++;
  650. spin_unlock(&mce_state_lock);
  651. unlock_kernel();
  652. return nonseekable_open(inode, file);
  653. }
  654. static int mce_release(struct inode *inode, struct file *file)
  655. {
  656. spin_lock(&mce_state_lock);
  657. open_count--;
  658. open_exclu = 0;
  659. spin_unlock(&mce_state_lock);
  660. return 0;
  661. }
  662. static void collect_tscs(void *data)
  663. {
  664. unsigned long *cpu_tsc = (unsigned long *)data;
  665. rdtscll(cpu_tsc[smp_processor_id()]);
  666. }
  667. static DEFINE_MUTEX(mce_read_mutex);
  668. static ssize_t mce_read(struct file *filp, char __user *ubuf, size_t usize,
  669. loff_t *off)
  670. {
  671. char __user *buf = ubuf;
  672. unsigned long *cpu_tsc;
  673. unsigned prev, next;
  674. int i, err;
  675. cpu_tsc = kmalloc(nr_cpu_ids * sizeof(long), GFP_KERNEL);
  676. if (!cpu_tsc)
  677. return -ENOMEM;
  678. mutex_lock(&mce_read_mutex);
  679. next = rcu_dereference(mcelog.next);
  680. /* Only supports full reads right now */
  681. if (*off != 0 || usize < MCE_LOG_LEN*sizeof(struct mce)) {
  682. mutex_unlock(&mce_read_mutex);
  683. kfree(cpu_tsc);
  684. return -EINVAL;
  685. }
  686. err = 0;
  687. prev = 0;
  688. do {
  689. for (i = prev; i < next; i++) {
  690. unsigned long start = jiffies;
  691. while (!mcelog.entry[i].finished) {
  692. if (time_after_eq(jiffies, start + 2)) {
  693. memset(mcelog.entry + i, 0,
  694. sizeof(struct mce));
  695. goto timeout;
  696. }
  697. cpu_relax();
  698. }
  699. smp_rmb();
  700. err |= copy_to_user(buf, mcelog.entry + i,
  701. sizeof(struct mce));
  702. buf += sizeof(struct mce);
  703. timeout:
  704. ;
  705. }
  706. memset(mcelog.entry + prev, 0,
  707. (next - prev) * sizeof(struct mce));
  708. prev = next;
  709. next = cmpxchg(&mcelog.next, prev, 0);
  710. } while (next != prev);
  711. synchronize_sched();
  712. /*
  713. * Collect entries that were still getting written before the
  714. * synchronize.
  715. */
  716. on_each_cpu(collect_tscs, cpu_tsc, 1);
  717. for (i = next; i < MCE_LOG_LEN; i++) {
  718. if (mcelog.entry[i].finished &&
  719. mcelog.entry[i].tsc < cpu_tsc[mcelog.entry[i].cpu]) {
  720. err |= copy_to_user(buf, mcelog.entry+i,
  721. sizeof(struct mce));
  722. smp_rmb();
  723. buf += sizeof(struct mce);
  724. memset(&mcelog.entry[i], 0, sizeof(struct mce));
  725. }
  726. }
  727. mutex_unlock(&mce_read_mutex);
  728. kfree(cpu_tsc);
  729. return err ? -EFAULT : buf - ubuf;
  730. }
  731. static unsigned int mce_poll(struct file *file, poll_table *wait)
  732. {
  733. poll_wait(file, &mce_wait, wait);
  734. if (rcu_dereference(mcelog.next))
  735. return POLLIN | POLLRDNORM;
  736. return 0;
  737. }
  738. static long mce_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
  739. {
  740. int __user *p = (int __user *)arg;
  741. if (!capable(CAP_SYS_ADMIN))
  742. return -EPERM;
  743. switch (cmd) {
  744. case MCE_GET_RECORD_LEN:
  745. return put_user(sizeof(struct mce), p);
  746. case MCE_GET_LOG_LEN:
  747. return put_user(MCE_LOG_LEN, p);
  748. case MCE_GETCLEAR_FLAGS: {
  749. unsigned flags;
  750. do {
  751. flags = mcelog.flags;
  752. } while (cmpxchg(&mcelog.flags, flags, 0) != flags);
  753. return put_user(flags, p);
  754. }
  755. default:
  756. return -ENOTTY;
  757. }
  758. }
  759. static const struct file_operations mce_chrdev_ops = {
  760. .open = mce_open,
  761. .release = mce_release,
  762. .read = mce_read,
  763. .poll = mce_poll,
  764. .unlocked_ioctl = mce_ioctl,
  765. };
  766. static struct miscdevice mce_log_device = {
  767. MISC_MCELOG_MINOR,
  768. "mcelog",
  769. &mce_chrdev_ops,
  770. };
  771. /*
  772. * Old style boot options parsing. Only for compatibility.
  773. */
  774. static int __init mcheck_disable(char *str)
  775. {
  776. mce_disabled = 1;
  777. return 1;
  778. }
  779. __setup("nomce", mcheck_disable);
  780. /*
  781. * mce=off disables machine check
  782. * mce=TOLERANCELEVEL (number, see above)
  783. * mce=bootlog Log MCEs from before booting. Disabled by default on AMD.
  784. * mce=nobootlog Don't log MCEs from before booting.
  785. */
  786. static int __init mcheck_enable(char *str)
  787. {
  788. if (!strcmp(str, "off"))
  789. mce_disabled = 1;
  790. else if (!strcmp(str, "bootlog") || !strcmp(str, "nobootlog"))
  791. mce_bootlog = (str[0] == 'b');
  792. else if (isdigit(str[0]))
  793. get_option(&str, &tolerant);
  794. else {
  795. printk(KERN_INFO "mce= argument %s ignored. Please use /sys\n",
  796. str);
  797. return 0;
  798. }
  799. return 1;
  800. }
  801. __setup("mce=", mcheck_enable);
  802. /*
  803. * Sysfs support
  804. */
  805. /*
  806. * Disable machine checks on suspend and shutdown. We can't really handle
  807. * them later.
  808. */
  809. static int mce_disable(void)
  810. {
  811. int i;
  812. for (i = 0; i < banks; i++) {
  813. if (!skip_bank_init(i))
  814. wrmsrl(MSR_IA32_MC0_CTL + i*4, 0);
  815. }
  816. return 0;
  817. }
  818. static int mce_suspend(struct sys_device *dev, pm_message_t state)
  819. {
  820. return mce_disable();
  821. }
  822. static int mce_shutdown(struct sys_device *dev)
  823. {
  824. return mce_disable();
  825. }
  826. /*
  827. * On resume clear all MCE state. Don't want to see leftovers from the BIOS.
  828. * Only one CPU is active at this time, the others get re-added later using
  829. * CPU hotplug:
  830. */
  831. static int mce_resume(struct sys_device *dev)
  832. {
  833. mce_init(NULL);
  834. mce_cpu_features(&current_cpu_data);
  835. return 0;
  836. }
  837. static void mce_cpu_restart(void *data)
  838. {
  839. del_timer_sync(&__get_cpu_var(mce_timer));
  840. if (mce_available(&current_cpu_data))
  841. mce_init(NULL);
  842. mce_init_timer();
  843. }
  844. /* Reinit MCEs after user configuration changes */
  845. static void mce_restart(void)
  846. {
  847. on_each_cpu(mce_cpu_restart, NULL, 1);
  848. }
  849. static struct sysdev_class mce_sysclass = {
  850. .suspend = mce_suspend,
  851. .shutdown = mce_shutdown,
  852. .resume = mce_resume,
  853. .name = "machinecheck",
  854. };
  855. DEFINE_PER_CPU(struct sys_device, mce_dev);
  856. __cpuinitdata
  857. void (*threshold_cpu_callback)(unsigned long action, unsigned int cpu);
  858. /* Why are there no generic functions for this? */
  859. #define ACCESSOR(name, var, start) \
  860. static ssize_t show_ ## name(struct sys_device *s, \
  861. struct sysdev_attribute *attr, \
  862. char *buf) { \
  863. return sprintf(buf, "%Lx\n", (u64)var); \
  864. } \
  865. static ssize_t set_ ## name(struct sys_device *s, \
  866. struct sysdev_attribute *attr, \
  867. const char *buf, size_t siz) { \
  868. char *end; \
  869. u64 new = simple_strtoull(buf, &end, 0); \
  870. \
  871. if (end == buf) \
  872. return -EINVAL; \
  873. var = new; \
  874. start; \
  875. \
  876. return end-buf; \
  877. } \
  878. static SYSDEV_ATTR(name, 0644, show_ ## name, set_ ## name);
  879. static struct sysdev_attribute *bank_attrs;
  880. static ssize_t show_bank(struct sys_device *s, struct sysdev_attribute *attr,
  881. char *buf)
  882. {
  883. u64 b = bank[attr - bank_attrs];
  884. return sprintf(buf, "%llx\n", b);
  885. }
  886. static ssize_t set_bank(struct sys_device *s, struct sysdev_attribute *attr,
  887. const char *buf, size_t siz)
  888. {
  889. char *end;
  890. u64 new = simple_strtoull(buf, &end, 0);
  891. if (end == buf)
  892. return -EINVAL;
  893. bank[attr - bank_attrs] = new;
  894. mce_restart();
  895. return end-buf;
  896. }
  897. static ssize_t
  898. show_trigger(struct sys_device *s, struct sysdev_attribute *attr, char *buf)
  899. {
  900. strcpy(buf, trigger);
  901. strcat(buf, "\n");
  902. return strlen(trigger) + 1;
  903. }
  904. static ssize_t set_trigger(struct sys_device *s, struct sysdev_attribute *attr,
  905. const char *buf, size_t siz)
  906. {
  907. char *p;
  908. int len;
  909. strncpy(trigger, buf, sizeof(trigger));
  910. trigger[sizeof(trigger)-1] = 0;
  911. len = strlen(trigger);
  912. p = strchr(trigger, '\n');
  913. if (*p)
  914. *p = 0;
  915. return len;
  916. }
  917. static SYSDEV_ATTR(trigger, 0644, show_trigger, set_trigger);
  918. static SYSDEV_INT_ATTR(tolerant, 0644, tolerant);
  919. ACCESSOR(check_interval, check_interval, mce_restart())
  920. static struct sysdev_attribute *mce_attrs[] = {
  921. &attr_tolerant.attr, &attr_check_interval, &attr_trigger,
  922. NULL
  923. };
  924. static cpumask_var_t mce_dev_initialized;
  925. /* Per cpu sysdev init. All of the cpus still share the same ctrl bank: */
  926. static __cpuinit int mce_create_device(unsigned int cpu)
  927. {
  928. int err;
  929. int i;
  930. if (!mce_available(&boot_cpu_data))
  931. return -EIO;
  932. memset(&per_cpu(mce_dev, cpu).kobj, 0, sizeof(struct kobject));
  933. per_cpu(mce_dev, cpu).id = cpu;
  934. per_cpu(mce_dev, cpu).cls = &mce_sysclass;
  935. err = sysdev_register(&per_cpu(mce_dev, cpu));
  936. if (err)
  937. return err;
  938. for (i = 0; mce_attrs[i]; i++) {
  939. err = sysdev_create_file(&per_cpu(mce_dev, cpu), mce_attrs[i]);
  940. if (err)
  941. goto error;
  942. }
  943. for (i = 0; i < banks; i++) {
  944. err = sysdev_create_file(&per_cpu(mce_dev, cpu),
  945. &bank_attrs[i]);
  946. if (err)
  947. goto error2;
  948. }
  949. cpumask_set_cpu(cpu, mce_dev_initialized);
  950. return 0;
  951. error2:
  952. while (--i >= 0)
  953. sysdev_remove_file(&per_cpu(mce_dev, cpu), &bank_attrs[i]);
  954. error:
  955. while (--i >= 0)
  956. sysdev_remove_file(&per_cpu(mce_dev, cpu), mce_attrs[i]);
  957. sysdev_unregister(&per_cpu(mce_dev, cpu));
  958. return err;
  959. }
  960. static __cpuinit void mce_remove_device(unsigned int cpu)
  961. {
  962. int i;
  963. if (!cpumask_test_cpu(cpu, mce_dev_initialized))
  964. return;
  965. for (i = 0; mce_attrs[i]; i++)
  966. sysdev_remove_file(&per_cpu(mce_dev, cpu), mce_attrs[i]);
  967. for (i = 0; i < banks; i++)
  968. sysdev_remove_file(&per_cpu(mce_dev, cpu), &bank_attrs[i]);
  969. sysdev_unregister(&per_cpu(mce_dev, cpu));
  970. cpumask_clear_cpu(cpu, mce_dev_initialized);
  971. }
  972. /* Make sure there are no machine checks on offlined CPUs. */
  973. static void mce_disable_cpu(void *h)
  974. {
  975. unsigned long action = *(unsigned long *)h;
  976. int i;
  977. if (!mce_available(&current_cpu_data))
  978. return;
  979. if (!(action & CPU_TASKS_FROZEN))
  980. cmci_clear();
  981. for (i = 0; i < banks; i++) {
  982. if (!skip_bank_init(i))
  983. wrmsrl(MSR_IA32_MC0_CTL + i*4, 0);
  984. }
  985. }
  986. static void mce_reenable_cpu(void *h)
  987. {
  988. unsigned long action = *(unsigned long *)h;
  989. int i;
  990. if (!mce_available(&current_cpu_data))
  991. return;
  992. if (!(action & CPU_TASKS_FROZEN))
  993. cmci_reenable();
  994. for (i = 0; i < banks; i++) {
  995. if (!skip_bank_init(i))
  996. wrmsrl(MSR_IA32_MC0_CTL + i*4, bank[i]);
  997. }
  998. }
  999. /* Get notified when a cpu comes on/off. Be hotplug friendly. */
  1000. static int __cpuinit
  1001. mce_cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
  1002. {
  1003. unsigned int cpu = (unsigned long)hcpu;
  1004. struct timer_list *t = &per_cpu(mce_timer, cpu);
  1005. switch (action) {
  1006. case CPU_ONLINE:
  1007. case CPU_ONLINE_FROZEN:
  1008. mce_create_device(cpu);
  1009. if (threshold_cpu_callback)
  1010. threshold_cpu_callback(action, cpu);
  1011. break;
  1012. case CPU_DEAD:
  1013. case CPU_DEAD_FROZEN:
  1014. if (threshold_cpu_callback)
  1015. threshold_cpu_callback(action, cpu);
  1016. mce_remove_device(cpu);
  1017. break;
  1018. case CPU_DOWN_PREPARE:
  1019. case CPU_DOWN_PREPARE_FROZEN:
  1020. del_timer_sync(t);
  1021. smp_call_function_single(cpu, mce_disable_cpu, &action, 1);
  1022. break;
  1023. case CPU_DOWN_FAILED:
  1024. case CPU_DOWN_FAILED_FROZEN:
  1025. t->expires = round_jiffies(jiffies +
  1026. __get_cpu_var(next_interval));
  1027. add_timer_on(t, cpu);
  1028. smp_call_function_single(cpu, mce_reenable_cpu, &action, 1);
  1029. break;
  1030. case CPU_POST_DEAD:
  1031. /* intentionally ignoring frozen here */
  1032. cmci_rediscover(cpu);
  1033. break;
  1034. }
  1035. return NOTIFY_OK;
  1036. }
  1037. static struct notifier_block mce_cpu_notifier __cpuinitdata = {
  1038. .notifier_call = mce_cpu_callback,
  1039. };
  1040. static __init int mce_init_banks(void)
  1041. {
  1042. int i;
  1043. bank_attrs = kzalloc(sizeof(struct sysdev_attribute) * banks,
  1044. GFP_KERNEL);
  1045. if (!bank_attrs)
  1046. return -ENOMEM;
  1047. for (i = 0; i < banks; i++) {
  1048. struct sysdev_attribute *a = &bank_attrs[i];
  1049. a->attr.name = kasprintf(GFP_KERNEL, "bank%d", i);
  1050. if (!a->attr.name)
  1051. goto nomem;
  1052. a->attr.mode = 0644;
  1053. a->show = show_bank;
  1054. a->store = set_bank;
  1055. }
  1056. return 0;
  1057. nomem:
  1058. while (--i >= 0)
  1059. kfree(bank_attrs[i].attr.name);
  1060. kfree(bank_attrs);
  1061. bank_attrs = NULL;
  1062. return -ENOMEM;
  1063. }
  1064. static __init int mce_init_device(void)
  1065. {
  1066. int err;
  1067. int i = 0;
  1068. if (!mce_available(&boot_cpu_data))
  1069. return -EIO;
  1070. alloc_cpumask_var(&mce_dev_initialized, GFP_KERNEL);
  1071. err = mce_init_banks();
  1072. if (err)
  1073. return err;
  1074. err = sysdev_class_register(&mce_sysclass);
  1075. if (err)
  1076. return err;
  1077. for_each_online_cpu(i) {
  1078. err = mce_create_device(i);
  1079. if (err)
  1080. return err;
  1081. }
  1082. register_hotcpu_notifier(&mce_cpu_notifier);
  1083. misc_register(&mce_log_device);
  1084. return err;
  1085. }
  1086. device_initcall(mce_init_device);
  1087. #else /* CONFIG_X86_32: */
  1088. int nr_mce_banks;
  1089. EXPORT_SYMBOL_GPL(nr_mce_banks); /* non-fatal.o */
  1090. /* This has to be run for each processor */
  1091. void mcheck_init(struct cpuinfo_x86 *c)
  1092. {
  1093. if (mce_disabled == 1)
  1094. return;
  1095. switch (c->x86_vendor) {
  1096. case X86_VENDOR_AMD:
  1097. amd_mcheck_init(c);
  1098. break;
  1099. case X86_VENDOR_INTEL:
  1100. if (c->x86 == 5)
  1101. intel_p5_mcheck_init(c);
  1102. if (c->x86 == 6)
  1103. intel_p6_mcheck_init(c);
  1104. if (c->x86 == 15)
  1105. intel_p4_mcheck_init(c);
  1106. break;
  1107. case X86_VENDOR_CENTAUR:
  1108. if (c->x86 == 5)
  1109. winchip_mcheck_init(c);
  1110. break;
  1111. default:
  1112. break;
  1113. }
  1114. printk(KERN_INFO "mce: CPU supports %d MCE banks\n", nr_mce_banks);
  1115. }
  1116. static int __init mcheck_disable(char *str)
  1117. {
  1118. mce_disabled = 1;
  1119. return 1;
  1120. }
  1121. static int __init mcheck_enable(char *str)
  1122. {
  1123. mce_disabled = -1;
  1124. return 1;
  1125. }
  1126. __setup("nomce", mcheck_disable);
  1127. __setup("mce", mcheck_enable);
  1128. #endif /* CONFIG_X86_32 */