mce.c 29 KB

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