mce_64.c 29 KB

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