mce.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386
  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/uaccess.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/smp.h>
  32. #include <linux/fs.h>
  33. #include <asm/processor.h>
  34. #include <asm/idle.h>
  35. #include <asm/mce.h>
  36. #include <asm/msr.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. DEFINE_PER_CPU(unsigned, mce_exception_count);
  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,
  110. (unsigned long *)&mcelog.flags);
  111. return;
  112. }
  113. /* Old left over entry. Skip: */
  114. if (mcelog.entry[entry].finished) {
  115. entry++;
  116. continue;
  117. }
  118. break;
  119. }
  120. smp_rmb();
  121. next = entry + 1;
  122. if (cmpxchg(&mcelog.next, entry, next) == entry)
  123. break;
  124. }
  125. memcpy(mcelog.entry + entry, mce, sizeof(struct mce));
  126. wmb();
  127. mcelog.entry[entry].finished = 1;
  128. wmb();
  129. set_bit(0, &notify_user);
  130. }
  131. static void print_mce(struct mce *m)
  132. {
  133. printk(KERN_EMERG "\n"
  134. KERN_EMERG "HARDWARE ERROR\n"
  135. KERN_EMERG
  136. "CPU %d: Machine Check Exception: %16Lx Bank %d: %016Lx\n",
  137. m->cpu, m->mcgstatus, m->bank, m->status);
  138. if (m->ip) {
  139. printk(KERN_EMERG "RIP%s %02x:<%016Lx> ",
  140. !(m->mcgstatus & MCG_STATUS_EIPV) ? " !INEXACT!" : "",
  141. m->cs, m->ip);
  142. if (m->cs == __KERNEL_CS)
  143. print_symbol("{%s}", m->ip);
  144. printk("\n");
  145. }
  146. printk(KERN_EMERG "TSC %llx ", m->tsc);
  147. if (m->addr)
  148. printk("ADDR %llx ", m->addr);
  149. if (m->misc)
  150. printk("MISC %llx ", m->misc);
  151. printk("\n");
  152. printk(KERN_EMERG "This is not a software problem!\n");
  153. printk(KERN_EMERG "Run through mcelog --ascii to decode "
  154. "and contact your hardware vendor\n");
  155. }
  156. static void mce_panic(char *msg, struct mce *backup, u64 start)
  157. {
  158. int i;
  159. bust_spinlocks(1);
  160. console_verbose();
  161. for (i = 0; i < MCE_LOG_LEN; i++) {
  162. u64 tsc = mcelog.entry[i].tsc;
  163. if ((s64)(tsc - start) < 0)
  164. continue;
  165. print_mce(&mcelog.entry[i]);
  166. if (backup && mcelog.entry[i].tsc == backup->tsc)
  167. backup = NULL;
  168. }
  169. if (backup)
  170. print_mce(backup);
  171. panic(msg);
  172. }
  173. /* Support code for software error injection */
  174. static int msr_to_offset(u32 msr)
  175. {
  176. unsigned bank = __get_cpu_var(injectm.bank);
  177. if (msr == rip_msr)
  178. return offsetof(struct mce, ip);
  179. if (msr == MSR_IA32_MC0_STATUS + bank*4)
  180. return offsetof(struct mce, status);
  181. if (msr == MSR_IA32_MC0_ADDR + bank*4)
  182. return offsetof(struct mce, addr);
  183. if (msr == MSR_IA32_MC0_MISC + bank*4)
  184. return offsetof(struct mce, misc);
  185. if (msr == MSR_IA32_MCG_STATUS)
  186. return offsetof(struct mce, mcgstatus);
  187. return -1;
  188. }
  189. /* MSR access wrappers used for error injection */
  190. static u64 mce_rdmsrl(u32 msr)
  191. {
  192. u64 v;
  193. if (__get_cpu_var(injectm).finished) {
  194. int offset = msr_to_offset(msr);
  195. if (offset < 0)
  196. return 0;
  197. return *(u64 *)((char *)&__get_cpu_var(injectm) + offset);
  198. }
  199. rdmsrl(msr, v);
  200. return v;
  201. }
  202. static void mce_wrmsrl(u32 msr, u64 v)
  203. {
  204. if (__get_cpu_var(injectm).finished) {
  205. int offset = msr_to_offset(msr);
  206. if (offset >= 0)
  207. *(u64 *)((char *)&__get_cpu_var(injectm) + offset) = v;
  208. return;
  209. }
  210. wrmsrl(msr, v);
  211. }
  212. int mce_available(struct cpuinfo_x86 *c)
  213. {
  214. if (mce_disabled)
  215. return 0;
  216. return cpu_has(c, X86_FEATURE_MCE) && cpu_has(c, X86_FEATURE_MCA);
  217. }
  218. static inline void mce_get_rip(struct mce *m, struct pt_regs *regs)
  219. {
  220. if (regs && (m->mcgstatus & MCG_STATUS_RIPV)) {
  221. m->ip = regs->ip;
  222. m->cs = regs->cs;
  223. } else {
  224. m->ip = 0;
  225. m->cs = 0;
  226. }
  227. if (rip_msr) {
  228. /* Assume the RIP in the MSR is exact. Is this true? */
  229. m->mcgstatus |= MCG_STATUS_EIPV;
  230. m->ip = mce_rdmsrl(rip_msr);
  231. m->cs = 0;
  232. }
  233. }
  234. DEFINE_PER_CPU(unsigned, mce_poll_count);
  235. /*
  236. * Poll for corrected events or events that happened before reset.
  237. * Those are just logged through /dev/mcelog.
  238. *
  239. * This is executed in standard interrupt context.
  240. */
  241. void machine_check_poll(enum mcp_flags flags, mce_banks_t *b)
  242. {
  243. struct mce m;
  244. int i;
  245. __get_cpu_var(mce_poll_count)++;
  246. mce_setup(&m);
  247. m.mcgstatus = mce_rdmsrl(MSR_IA32_MCG_STATUS);
  248. for (i = 0; i < banks; i++) {
  249. if (!bank[i] || !test_bit(i, *b))
  250. continue;
  251. m.misc = 0;
  252. m.addr = 0;
  253. m.bank = i;
  254. m.tsc = 0;
  255. barrier();
  256. m.status = mce_rdmsrl(MSR_IA32_MC0_STATUS + i*4);
  257. if (!(m.status & MCI_STATUS_VAL))
  258. continue;
  259. /*
  260. * Uncorrected events are handled by the exception handler
  261. * when it is enabled. But when the exception is disabled log
  262. * everything.
  263. *
  264. * TBD do the same check for MCI_STATUS_EN here?
  265. */
  266. if ((m.status & MCI_STATUS_UC) && !(flags & MCP_UC))
  267. continue;
  268. if (m.status & MCI_STATUS_MISCV)
  269. m.misc = mce_rdmsrl(MSR_IA32_MC0_MISC + i*4);
  270. if (m.status & MCI_STATUS_ADDRV)
  271. m.addr = mce_rdmsrl(MSR_IA32_MC0_ADDR + i*4);
  272. if (!(flags & MCP_TIMESTAMP))
  273. m.tsc = 0;
  274. /*
  275. * Don't get the IP here because it's unlikely to
  276. * have anything to do with the actual error location.
  277. */
  278. if (!(flags & MCP_DONTLOG)) {
  279. mce_log(&m);
  280. add_taint(TAINT_MACHINE_CHECK);
  281. }
  282. /*
  283. * Clear state for this bank.
  284. */
  285. mce_wrmsrl(MSR_IA32_MC0_STATUS+4*i, 0);
  286. }
  287. /*
  288. * Don't clear MCG_STATUS here because it's only defined for
  289. * exceptions.
  290. */
  291. sync_core();
  292. }
  293. EXPORT_SYMBOL_GPL(machine_check_poll);
  294. /*
  295. * The actual machine check handler. This only handles real
  296. * exceptions when something got corrupted coming in through int 18.
  297. *
  298. * This is executed in NMI context not subject to normal locking rules. This
  299. * implies that most kernel services cannot be safely used. Don't even
  300. * think about putting a printk in there!
  301. */
  302. void do_machine_check(struct pt_regs *regs, long error_code)
  303. {
  304. struct mce m, panicm;
  305. int panicm_found = 0;
  306. u64 mcestart = 0;
  307. int i;
  308. /*
  309. * If no_way_out gets set, there is no safe way to recover from this
  310. * MCE. If tolerant is cranked up, we'll try anyway.
  311. */
  312. int no_way_out = 0;
  313. /*
  314. * If kill_it gets set, there might be a way to recover from this
  315. * error.
  316. */
  317. int kill_it = 0;
  318. DECLARE_BITMAP(toclear, MAX_NR_BANKS);
  319. atomic_inc(&mce_entry);
  320. __get_cpu_var(mce_exception_count)++;
  321. if (notify_die(DIE_NMI, "machine check", regs, error_code,
  322. 18, SIGKILL) == NOTIFY_STOP)
  323. goto out;
  324. if (!banks)
  325. goto out;
  326. mce_setup(&m);
  327. m.mcgstatus = mce_rdmsrl(MSR_IA32_MCG_STATUS);
  328. /* if the restart IP is not valid, we're done for */
  329. if (!(m.mcgstatus & MCG_STATUS_RIPV))
  330. no_way_out = 1;
  331. rdtscll(mcestart);
  332. barrier();
  333. for (i = 0; i < banks; i++) {
  334. __clear_bit(i, toclear);
  335. if (!bank[i])
  336. continue;
  337. m.misc = 0;
  338. m.addr = 0;
  339. m.bank = i;
  340. m.status = mce_rdmsrl(MSR_IA32_MC0_STATUS + i*4);
  341. if ((m.status & MCI_STATUS_VAL) == 0)
  342. continue;
  343. /*
  344. * Non uncorrected errors are handled by machine_check_poll
  345. * Leave them alone.
  346. */
  347. if ((m.status & MCI_STATUS_UC) == 0)
  348. continue;
  349. /*
  350. * Set taint even when machine check was not enabled.
  351. */
  352. add_taint(TAINT_MACHINE_CHECK);
  353. __set_bit(i, toclear);
  354. if (m.status & MCI_STATUS_EN) {
  355. /* if PCC was set, there's no way out */
  356. no_way_out |= !!(m.status & MCI_STATUS_PCC);
  357. /*
  358. * If this error was uncorrectable and there was
  359. * an overflow, we're in trouble. If no overflow,
  360. * we might get away with just killing a task.
  361. */
  362. if (m.status & MCI_STATUS_UC) {
  363. if (tolerant < 1 || m.status & MCI_STATUS_OVER)
  364. no_way_out = 1;
  365. kill_it = 1;
  366. }
  367. } else {
  368. /*
  369. * Machine check event was not enabled. Clear, but
  370. * ignore.
  371. */
  372. continue;
  373. }
  374. if (m.status & MCI_STATUS_MISCV)
  375. m.misc = mce_rdmsrl(MSR_IA32_MC0_MISC + i*4);
  376. if (m.status & MCI_STATUS_ADDRV)
  377. m.addr = mce_rdmsrl(MSR_IA32_MC0_ADDR + i*4);
  378. mce_get_rip(&m, regs);
  379. mce_log(&m);
  380. /*
  381. * Did this bank cause the exception?
  382. *
  383. * Assume that the bank with uncorrectable errors did it,
  384. * and that there is only a single one:
  385. */
  386. if ((m.status & MCI_STATUS_UC) &&
  387. (m.status & MCI_STATUS_EN)) {
  388. panicm = m;
  389. panicm_found = 1;
  390. }
  391. }
  392. /*
  393. * If we didn't find an uncorrectable error, pick
  394. * the last one (shouldn't happen, just being safe).
  395. */
  396. if (!panicm_found)
  397. panicm = m;
  398. /*
  399. * If we have decided that we just CAN'T continue, and the user
  400. * has not set tolerant to an insane level, give up and die.
  401. */
  402. if (no_way_out && tolerant < 3)
  403. mce_panic("Machine check", &panicm, mcestart);
  404. /*
  405. * If the error seems to be unrecoverable, something should be
  406. * done. Try to kill as little as possible. If we can kill just
  407. * one task, do that. If the user has set the tolerance very
  408. * high, don't try to do anything at all.
  409. */
  410. if (kill_it && tolerant < 3) {
  411. int user_space = 0;
  412. /*
  413. * If the EIPV bit is set, it means the saved IP is the
  414. * instruction which caused the MCE.
  415. */
  416. if (m.mcgstatus & MCG_STATUS_EIPV)
  417. user_space = panicm.ip && (panicm.cs & 3);
  418. /*
  419. * If we know that the error was in user space, send a
  420. * SIGBUS. Otherwise, panic if tolerance is low.
  421. *
  422. * force_sig() takes an awful lot of locks and has a slight
  423. * risk of deadlocking.
  424. */
  425. if (user_space) {
  426. force_sig(SIGBUS, current);
  427. } else if (panic_on_oops || tolerant < 2) {
  428. mce_panic("Uncorrected machine check",
  429. &panicm, mcestart);
  430. }
  431. }
  432. /* notify userspace ASAP */
  433. set_thread_flag(TIF_MCE_NOTIFY);
  434. /* the last thing we do is clear state */
  435. for (i = 0; i < banks; i++) {
  436. if (test_bit(i, toclear))
  437. mce_wrmsrl(MSR_IA32_MC0_STATUS+4*i, 0);
  438. }
  439. mce_wrmsrl(MSR_IA32_MCG_STATUS, 0);
  440. out:
  441. atomic_dec(&mce_entry);
  442. sync_core();
  443. }
  444. EXPORT_SYMBOL_GPL(do_machine_check);
  445. #ifdef CONFIG_X86_MCE_INTEL
  446. /***
  447. * mce_log_therm_throt_event - Logs the thermal throttling event to mcelog
  448. * @cpu: The CPU on which the event occurred.
  449. * @status: Event status information
  450. *
  451. * This function should be called by the thermal interrupt after the
  452. * event has been processed and the decision was made to log the event
  453. * further.
  454. *
  455. * The status parameter will be saved to the 'status' field of 'struct mce'
  456. * and historically has been the register value of the
  457. * MSR_IA32_THERMAL_STATUS (Intel) msr.
  458. */
  459. void mce_log_therm_throt_event(__u64 status)
  460. {
  461. struct mce m;
  462. mce_setup(&m);
  463. m.bank = MCE_THERMAL_BANK;
  464. m.status = status;
  465. mce_log(&m);
  466. }
  467. #endif /* CONFIG_X86_MCE_INTEL */
  468. /*
  469. * Periodic polling timer for "silent" machine check errors. If the
  470. * poller finds an MCE, poll 2x faster. When the poller finds no more
  471. * errors, poll 2x slower (up to check_interval seconds).
  472. */
  473. static int check_interval = 5 * 60; /* 5 minutes */
  474. static DEFINE_PER_CPU(int, next_interval); /* in jiffies */
  475. static DEFINE_PER_CPU(struct timer_list, mce_timer);
  476. static void mcheck_timer(unsigned long data)
  477. {
  478. struct timer_list *t = &per_cpu(mce_timer, data);
  479. int *n;
  480. WARN_ON(smp_processor_id() != data);
  481. if (mce_available(&current_cpu_data)) {
  482. machine_check_poll(MCP_TIMESTAMP,
  483. &__get_cpu_var(mce_poll_banks));
  484. }
  485. /*
  486. * Alert userspace if needed. If we logged an MCE, reduce the
  487. * polling interval, otherwise increase the polling interval.
  488. */
  489. n = &__get_cpu_var(next_interval);
  490. if (mce_notify_user())
  491. *n = max(*n/2, HZ/100);
  492. else
  493. *n = min(*n*2, (int)round_jiffies_relative(check_interval*HZ));
  494. t->expires = jiffies + *n;
  495. add_timer(t);
  496. }
  497. static void mce_do_trigger(struct work_struct *work)
  498. {
  499. call_usermodehelper(trigger, trigger_argv, NULL, UMH_NO_WAIT);
  500. }
  501. static DECLARE_WORK(mce_trigger_work, mce_do_trigger);
  502. /*
  503. * Notify the user(s) about new machine check events.
  504. * Can be called from interrupt context, but not from machine check/NMI
  505. * context.
  506. */
  507. int mce_notify_user(void)
  508. {
  509. /* Not more than two messages every minute */
  510. static DEFINE_RATELIMIT_STATE(ratelimit, 60*HZ, 2);
  511. clear_thread_flag(TIF_MCE_NOTIFY);
  512. if (test_and_clear_bit(0, &notify_user)) {
  513. wake_up_interruptible(&mce_wait);
  514. /*
  515. * There is no risk of missing notifications because
  516. * work_pending is always cleared before the function is
  517. * executed.
  518. */
  519. if (trigger[0] && !work_pending(&mce_trigger_work))
  520. schedule_work(&mce_trigger_work);
  521. if (__ratelimit(&ratelimit))
  522. printk(KERN_INFO "Machine check events logged\n");
  523. return 1;
  524. }
  525. return 0;
  526. }
  527. EXPORT_SYMBOL_GPL(mce_notify_user);
  528. /*
  529. * Initialize Machine Checks for a CPU.
  530. */
  531. static int mce_cap_init(void)
  532. {
  533. unsigned b;
  534. u64 cap;
  535. rdmsrl(MSR_IA32_MCG_CAP, cap);
  536. b = cap & MCG_BANKCNT_MASK;
  537. printk(KERN_INFO "mce: CPU supports %d MCE banks\n", b);
  538. if (b > MAX_NR_BANKS) {
  539. printk(KERN_WARNING
  540. "MCE: Using only %u machine check banks out of %u\n",
  541. MAX_NR_BANKS, b);
  542. b = MAX_NR_BANKS;
  543. }
  544. /* Don't support asymmetric configurations today */
  545. WARN_ON(banks != 0 && b != banks);
  546. banks = b;
  547. if (!bank) {
  548. bank = kmalloc(banks * sizeof(u64), GFP_KERNEL);
  549. if (!bank)
  550. return -ENOMEM;
  551. memset(bank, 0xff, banks * sizeof(u64));
  552. }
  553. /* Use accurate RIP reporting if available. */
  554. if ((cap & MCG_EXT_P) && MCG_EXT_CNT(cap) >= 9)
  555. rip_msr = MSR_IA32_MCG_EIP;
  556. return 0;
  557. }
  558. static void mce_init(void)
  559. {
  560. mce_banks_t all_banks;
  561. u64 cap;
  562. int i;
  563. /*
  564. * Log the machine checks left over from the previous reset.
  565. */
  566. bitmap_fill(all_banks, MAX_NR_BANKS);
  567. machine_check_poll(MCP_UC|(!mce_bootlog ? MCP_DONTLOG : 0), &all_banks);
  568. set_in_cr4(X86_CR4_MCE);
  569. rdmsrl(MSR_IA32_MCG_CAP, cap);
  570. if (cap & MCG_CTL_P)
  571. wrmsr(MSR_IA32_MCG_CTL, 0xffffffff, 0xffffffff);
  572. for (i = 0; i < banks; i++) {
  573. if (skip_bank_init(i))
  574. continue;
  575. wrmsrl(MSR_IA32_MC0_CTL+4*i, bank[i]);
  576. wrmsrl(MSR_IA32_MC0_STATUS+4*i, 0);
  577. }
  578. }
  579. /* Add per CPU specific workarounds here */
  580. static void mce_cpu_quirks(struct cpuinfo_x86 *c)
  581. {
  582. /* This should be disabled by the BIOS, but isn't always */
  583. if (c->x86_vendor == X86_VENDOR_AMD) {
  584. if (c->x86 == 15 && banks > 4) {
  585. /*
  586. * disable GART TBL walk error reporting, which
  587. * trips off incorrectly with the IOMMU & 3ware
  588. * & Cerberus:
  589. */
  590. clear_bit(10, (unsigned long *)&bank[4]);
  591. }
  592. if (c->x86 <= 17 && mce_bootlog < 0) {
  593. /*
  594. * Lots of broken BIOS around that don't clear them
  595. * by default and leave crap in there. Don't log:
  596. */
  597. mce_bootlog = 0;
  598. }
  599. /*
  600. * Various K7s with broken bank 0 around. Always disable
  601. * by default.
  602. */
  603. if (c->x86 == 6)
  604. bank[0] = 0;
  605. }
  606. if (c->x86_vendor == X86_VENDOR_INTEL) {
  607. /*
  608. * SDM documents that on family 6 bank 0 should not be written
  609. * because it aliases to another special BIOS controlled
  610. * register.
  611. * But it's not aliased anymore on model 0x1a+
  612. * Don't ignore bank 0 completely because there could be a
  613. * valid event later, merely don't write CTL0.
  614. */
  615. if (c->x86 == 6 && c->x86_model < 0x1A)
  616. __set_bit(0, &dont_init_banks);
  617. }
  618. }
  619. static void __cpuinit mce_ancient_init(struct cpuinfo_x86 *c)
  620. {
  621. if (c->x86 != 5)
  622. return;
  623. switch (c->x86_vendor) {
  624. case X86_VENDOR_INTEL:
  625. if (mce_p5_enabled())
  626. intel_p5_mcheck_init(c);
  627. break;
  628. case X86_VENDOR_CENTAUR:
  629. winchip_mcheck_init(c);
  630. break;
  631. }
  632. }
  633. static void mce_cpu_features(struct cpuinfo_x86 *c)
  634. {
  635. switch (c->x86_vendor) {
  636. case X86_VENDOR_INTEL:
  637. mce_intel_feature_init(c);
  638. break;
  639. case X86_VENDOR_AMD:
  640. mce_amd_feature_init(c);
  641. break;
  642. default:
  643. break;
  644. }
  645. }
  646. static void mce_init_timer(void)
  647. {
  648. struct timer_list *t = &__get_cpu_var(mce_timer);
  649. int *n = &__get_cpu_var(next_interval);
  650. *n = check_interval * HZ;
  651. if (!*n)
  652. return;
  653. setup_timer(t, mcheck_timer, smp_processor_id());
  654. t->expires = round_jiffies(jiffies + *n);
  655. add_timer(t);
  656. }
  657. /*
  658. * Called for each booted CPU to set up machine checks.
  659. * Must be called with preempt off:
  660. */
  661. void __cpuinit mcheck_init(struct cpuinfo_x86 *c)
  662. {
  663. if (mce_disabled)
  664. return;
  665. mce_ancient_init(c);
  666. if (!mce_available(c))
  667. return;
  668. if (mce_cap_init() < 0) {
  669. mce_disabled = 1;
  670. return;
  671. }
  672. mce_cpu_quirks(c);
  673. machine_check_vector = do_machine_check;
  674. mce_init();
  675. mce_cpu_features(c);
  676. mce_init_timer();
  677. }
  678. /*
  679. * Character device to read and clear the MCE log.
  680. */
  681. static DEFINE_SPINLOCK(mce_state_lock);
  682. static int open_count; /* #times opened */
  683. static int open_exclu; /* already open exclusive? */
  684. static int mce_open(struct inode *inode, struct file *file)
  685. {
  686. spin_lock(&mce_state_lock);
  687. if (open_exclu || (open_count && (file->f_flags & O_EXCL))) {
  688. spin_unlock(&mce_state_lock);
  689. return -EBUSY;
  690. }
  691. if (file->f_flags & O_EXCL)
  692. open_exclu = 1;
  693. open_count++;
  694. spin_unlock(&mce_state_lock);
  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 size)
  907. {
  908. u64 new;
  909. if (strict_strtoull(buf, 0, &new) < 0)
  910. return -EINVAL;
  911. bank[attr - bank_attrs] = new;
  912. mce_restart();
  913. return size;
  914. }
  915. static ssize_t
  916. show_trigger(struct sys_device *s, struct sysdev_attribute *attr, char *buf)
  917. {
  918. strcpy(buf, trigger);
  919. strcat(buf, "\n");
  920. return strlen(trigger) + 1;
  921. }
  922. static ssize_t set_trigger(struct sys_device *s, struct sysdev_attribute *attr,
  923. const char *buf, size_t siz)
  924. {
  925. char *p;
  926. int len;
  927. strncpy(trigger, buf, sizeof(trigger));
  928. trigger[sizeof(trigger)-1] = 0;
  929. len = strlen(trigger);
  930. p = strchr(trigger, '\n');
  931. if (*p)
  932. *p = 0;
  933. return len;
  934. }
  935. static ssize_t store_int_with_restart(struct sys_device *s,
  936. struct sysdev_attribute *attr,
  937. const char *buf, size_t size)
  938. {
  939. ssize_t ret = sysdev_store_int(s, attr, buf, size);
  940. mce_restart();
  941. return ret;
  942. }
  943. static SYSDEV_ATTR(trigger, 0644, show_trigger, set_trigger);
  944. static SYSDEV_INT_ATTR(tolerant, 0644, tolerant);
  945. static struct sysdev_ext_attribute attr_check_interval = {
  946. _SYSDEV_ATTR(check_interval, 0644, sysdev_show_int,
  947. store_int_with_restart),
  948. &check_interval
  949. };
  950. static struct sysdev_attribute *mce_attrs[] = {
  951. &attr_tolerant.attr, &attr_check_interval.attr, &attr_trigger,
  952. NULL
  953. };
  954. static cpumask_var_t mce_dev_initialized;
  955. /* Per cpu sysdev init. All of the cpus still share the same ctrl bank: */
  956. static __cpuinit int mce_create_device(unsigned int cpu)
  957. {
  958. int err;
  959. int i;
  960. if (!mce_available(&boot_cpu_data))
  961. return -EIO;
  962. memset(&per_cpu(mce_dev, cpu).kobj, 0, sizeof(struct kobject));
  963. per_cpu(mce_dev, cpu).id = cpu;
  964. per_cpu(mce_dev, cpu).cls = &mce_sysclass;
  965. err = sysdev_register(&per_cpu(mce_dev, cpu));
  966. if (err)
  967. return err;
  968. for (i = 0; mce_attrs[i]; i++) {
  969. err = sysdev_create_file(&per_cpu(mce_dev, cpu), mce_attrs[i]);
  970. if (err)
  971. goto error;
  972. }
  973. for (i = 0; i < banks; i++) {
  974. err = sysdev_create_file(&per_cpu(mce_dev, cpu),
  975. &bank_attrs[i]);
  976. if (err)
  977. goto error2;
  978. }
  979. cpumask_set_cpu(cpu, mce_dev_initialized);
  980. return 0;
  981. error2:
  982. while (--i >= 0)
  983. sysdev_remove_file(&per_cpu(mce_dev, cpu), &bank_attrs[i]);
  984. error:
  985. while (--i >= 0)
  986. sysdev_remove_file(&per_cpu(mce_dev, cpu), mce_attrs[i]);
  987. sysdev_unregister(&per_cpu(mce_dev, cpu));
  988. return err;
  989. }
  990. static __cpuinit void mce_remove_device(unsigned int cpu)
  991. {
  992. int i;
  993. if (!cpumask_test_cpu(cpu, mce_dev_initialized))
  994. return;
  995. for (i = 0; mce_attrs[i]; i++)
  996. sysdev_remove_file(&per_cpu(mce_dev, cpu), mce_attrs[i]);
  997. for (i = 0; i < banks; i++)
  998. sysdev_remove_file(&per_cpu(mce_dev, cpu), &bank_attrs[i]);
  999. sysdev_unregister(&per_cpu(mce_dev, cpu));
  1000. cpumask_clear_cpu(cpu, mce_dev_initialized);
  1001. }
  1002. /* Make sure there are no machine checks on offlined CPUs. */
  1003. static void mce_disable_cpu(void *h)
  1004. {
  1005. unsigned long action = *(unsigned long *)h;
  1006. int i;
  1007. if (!mce_available(&current_cpu_data))
  1008. return;
  1009. if (!(action & CPU_TASKS_FROZEN))
  1010. cmci_clear();
  1011. for (i = 0; i < banks; i++) {
  1012. if (!skip_bank_init(i))
  1013. wrmsrl(MSR_IA32_MC0_CTL + i*4, 0);
  1014. }
  1015. }
  1016. static void mce_reenable_cpu(void *h)
  1017. {
  1018. unsigned long action = *(unsigned long *)h;
  1019. int i;
  1020. if (!mce_available(&current_cpu_data))
  1021. return;
  1022. if (!(action & CPU_TASKS_FROZEN))
  1023. cmci_reenable();
  1024. for (i = 0; i < banks; i++) {
  1025. if (!skip_bank_init(i))
  1026. wrmsrl(MSR_IA32_MC0_CTL + i*4, bank[i]);
  1027. }
  1028. }
  1029. /* Get notified when a cpu comes on/off. Be hotplug friendly. */
  1030. static int __cpuinit
  1031. mce_cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
  1032. {
  1033. unsigned int cpu = (unsigned long)hcpu;
  1034. struct timer_list *t = &per_cpu(mce_timer, cpu);
  1035. switch (action) {
  1036. case CPU_ONLINE:
  1037. case CPU_ONLINE_FROZEN:
  1038. mce_create_device(cpu);
  1039. if (threshold_cpu_callback)
  1040. threshold_cpu_callback(action, cpu);
  1041. break;
  1042. case CPU_DEAD:
  1043. case CPU_DEAD_FROZEN:
  1044. if (threshold_cpu_callback)
  1045. threshold_cpu_callback(action, cpu);
  1046. mce_remove_device(cpu);
  1047. break;
  1048. case CPU_DOWN_PREPARE:
  1049. case CPU_DOWN_PREPARE_FROZEN:
  1050. del_timer_sync(t);
  1051. smp_call_function_single(cpu, mce_disable_cpu, &action, 1);
  1052. break;
  1053. case CPU_DOWN_FAILED:
  1054. case CPU_DOWN_FAILED_FROZEN:
  1055. t->expires = round_jiffies(jiffies +
  1056. __get_cpu_var(next_interval));
  1057. add_timer_on(t, cpu);
  1058. smp_call_function_single(cpu, mce_reenable_cpu, &action, 1);
  1059. break;
  1060. case CPU_POST_DEAD:
  1061. /* intentionally ignoring frozen here */
  1062. cmci_rediscover(cpu);
  1063. break;
  1064. }
  1065. return NOTIFY_OK;
  1066. }
  1067. static struct notifier_block mce_cpu_notifier __cpuinitdata = {
  1068. .notifier_call = mce_cpu_callback,
  1069. };
  1070. static __init int mce_init_banks(void)
  1071. {
  1072. int i;
  1073. bank_attrs = kzalloc(sizeof(struct sysdev_attribute) * banks,
  1074. GFP_KERNEL);
  1075. if (!bank_attrs)
  1076. return -ENOMEM;
  1077. for (i = 0; i < banks; i++) {
  1078. struct sysdev_attribute *a = &bank_attrs[i];
  1079. a->attr.name = kasprintf(GFP_KERNEL, "bank%d", i);
  1080. if (!a->attr.name)
  1081. goto nomem;
  1082. a->attr.mode = 0644;
  1083. a->show = show_bank;
  1084. a->store = set_bank;
  1085. }
  1086. return 0;
  1087. nomem:
  1088. while (--i >= 0)
  1089. kfree(bank_attrs[i].attr.name);
  1090. kfree(bank_attrs);
  1091. bank_attrs = NULL;
  1092. return -ENOMEM;
  1093. }
  1094. static __init int mce_init_device(void)
  1095. {
  1096. int err;
  1097. int i = 0;
  1098. if (!mce_available(&boot_cpu_data))
  1099. return -EIO;
  1100. alloc_cpumask_var(&mce_dev_initialized, GFP_KERNEL);
  1101. err = mce_init_banks();
  1102. if (err)
  1103. return err;
  1104. err = sysdev_class_register(&mce_sysclass);
  1105. if (err)
  1106. return err;
  1107. for_each_online_cpu(i) {
  1108. err = mce_create_device(i);
  1109. if (err)
  1110. return err;
  1111. }
  1112. register_hotcpu_notifier(&mce_cpu_notifier);
  1113. misc_register(&mce_log_device);
  1114. return err;
  1115. }
  1116. device_initcall(mce_init_device);
  1117. #else /* CONFIG_X86_OLD_MCE: */
  1118. int nr_mce_banks;
  1119. EXPORT_SYMBOL_GPL(nr_mce_banks); /* non-fatal.o */
  1120. /* This has to be run for each processor */
  1121. void mcheck_init(struct cpuinfo_x86 *c)
  1122. {
  1123. if (mce_disabled == 1)
  1124. return;
  1125. switch (c->x86_vendor) {
  1126. case X86_VENDOR_AMD:
  1127. amd_mcheck_init(c);
  1128. break;
  1129. case X86_VENDOR_INTEL:
  1130. if (c->x86 == 5)
  1131. intel_p5_mcheck_init(c);
  1132. if (c->x86 == 6)
  1133. intel_p6_mcheck_init(c);
  1134. if (c->x86 == 15)
  1135. intel_p4_mcheck_init(c);
  1136. break;
  1137. case X86_VENDOR_CENTAUR:
  1138. if (c->x86 == 5)
  1139. winchip_mcheck_init(c);
  1140. break;
  1141. default:
  1142. break;
  1143. }
  1144. printk(KERN_INFO "mce: CPU supports %d MCE banks\n", nr_mce_banks);
  1145. }
  1146. static int __init mcheck_enable(char *str)
  1147. {
  1148. mce_disabled = -1;
  1149. return 1;
  1150. }
  1151. __setup("mce", mcheck_enable);
  1152. #endif /* CONFIG_X86_OLD_MCE */
  1153. /*
  1154. * Old style boot options parsing. Only for compatibility.
  1155. */
  1156. static int __init mcheck_disable(char *str)
  1157. {
  1158. mce_disabled = 1;
  1159. return 1;
  1160. }
  1161. __setup("nomce", mcheck_disable);