mce.c 30 KB

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