mce.c 29 KB

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