mce.c 29 KB

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