mce.c 30 KB

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