mce.c 30 KB

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