mce.c 30 KB

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