mce.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812
  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/interrupt.h>
  14. #include <linux/ratelimit.h>
  15. #include <linux/kallsyms.h>
  16. #include <linux/rcupdate.h>
  17. #include <linux/kobject.h>
  18. #include <linux/uaccess.h>
  19. #include <linux/kdebug.h>
  20. #include <linux/kernel.h>
  21. #include <linux/percpu.h>
  22. #include <linux/string.h>
  23. #include <linux/sysdev.h>
  24. #include <linux/delay.h>
  25. #include <linux/ctype.h>
  26. #include <linux/sched.h>
  27. #include <linux/sysfs.h>
  28. #include <linux/types.h>
  29. #include <linux/init.h>
  30. #include <linux/kmod.h>
  31. #include <linux/poll.h>
  32. #include <linux/nmi.h>
  33. #include <linux/cpu.h>
  34. #include <linux/smp.h>
  35. #include <linux/fs.h>
  36. #include <asm/processor.h>
  37. #include <asm/hw_irq.h>
  38. #include <asm/apic.h>
  39. #include <asm/idle.h>
  40. #include <asm/ipi.h>
  41. #include <asm/mce.h>
  42. #include <asm/msr.h>
  43. #include "mce-internal.h"
  44. #include "mce.h"
  45. /* Handle unconfigured int18 (should never happen) */
  46. static void unexpected_machine_check(struct pt_regs *regs, long error_code)
  47. {
  48. printk(KERN_ERR "CPU#%d: Unexpected int18 (Machine Check).\n",
  49. smp_processor_id());
  50. }
  51. /* Call the installed machine check handler for this CPU setup. */
  52. void (*machine_check_vector)(struct pt_regs *, long error_code) =
  53. unexpected_machine_check;
  54. int mce_disabled;
  55. #ifdef CONFIG_X86_NEW_MCE
  56. #define MISC_MCELOG_MINOR 227
  57. #define SPINUNIT 100 /* 100ns */
  58. atomic_t mce_entry;
  59. DEFINE_PER_CPU(unsigned, mce_exception_count);
  60. /*
  61. * Tolerant levels:
  62. * 0: always panic on uncorrected errors, log corrected errors
  63. * 1: panic or SIGBUS on uncorrected errors, log corrected errors
  64. * 2: SIGBUS or log uncorrected errors (if possible), log corrected errors
  65. * 3: never panic or SIGBUS, log all errors (for testing only)
  66. */
  67. static int tolerant = 1;
  68. static int banks;
  69. static u64 *bank;
  70. static unsigned long notify_user;
  71. static int rip_msr;
  72. static int mce_bootlog = -1;
  73. static int monarch_timeout = -1;
  74. static int mce_panic_timeout;
  75. int mce_ser;
  76. static char trigger[128];
  77. static char *trigger_argv[2] = { trigger, NULL };
  78. static unsigned long dont_init_banks;
  79. static DECLARE_WAIT_QUEUE_HEAD(mce_wait);
  80. static DEFINE_PER_CPU(struct mce, mces_seen);
  81. static int cpu_missing;
  82. /* MCA banks polled by the period polling timer for corrected events */
  83. DEFINE_PER_CPU(mce_banks_t, mce_poll_banks) = {
  84. [0 ... BITS_TO_LONGS(MAX_NR_BANKS)-1] = ~0UL
  85. };
  86. static inline int skip_bank_init(int i)
  87. {
  88. return i < BITS_PER_LONG && test_bit(i, &dont_init_banks);
  89. }
  90. /* Do initial initialization of a struct mce */
  91. void mce_setup(struct mce *m)
  92. {
  93. memset(m, 0, sizeof(struct mce));
  94. m->cpu = m->extcpu = smp_processor_id();
  95. rdtscll(m->tsc);
  96. /* We hope get_seconds stays lockless */
  97. m->time = get_seconds();
  98. m->cpuvendor = boot_cpu_data.x86_vendor;
  99. m->cpuid = cpuid_eax(1);
  100. #ifdef CONFIG_SMP
  101. m->socketid = cpu_data(m->extcpu).phys_proc_id;
  102. #endif
  103. m->apicid = cpu_data(m->extcpu).initial_apicid;
  104. rdmsrl(MSR_IA32_MCG_CAP, m->mcgcap);
  105. }
  106. DEFINE_PER_CPU(struct mce, injectm);
  107. EXPORT_PER_CPU_SYMBOL_GPL(injectm);
  108. /*
  109. * Lockless MCE logging infrastructure.
  110. * This avoids deadlocks on printk locks without having to break locks. Also
  111. * separate MCEs from kernel messages to avoid bogus bug reports.
  112. */
  113. static struct mce_log mcelog = {
  114. .signature = MCE_LOG_SIGNATURE,
  115. .len = MCE_LOG_LEN,
  116. .recordlen = sizeof(struct mce),
  117. };
  118. void mce_log(struct mce *mce)
  119. {
  120. unsigned next, entry;
  121. mce->finished = 0;
  122. wmb();
  123. for (;;) {
  124. entry = rcu_dereference(mcelog.next);
  125. for (;;) {
  126. /*
  127. * When the buffer fills up discard new entries.
  128. * Assume that the earlier errors are the more
  129. * interesting ones:
  130. */
  131. if (entry >= MCE_LOG_LEN) {
  132. set_bit(MCE_OVERFLOW,
  133. (unsigned long *)&mcelog.flags);
  134. return;
  135. }
  136. /* Old left over entry. Skip: */
  137. if (mcelog.entry[entry].finished) {
  138. entry++;
  139. continue;
  140. }
  141. break;
  142. }
  143. smp_rmb();
  144. next = entry + 1;
  145. if (cmpxchg(&mcelog.next, entry, next) == entry)
  146. break;
  147. }
  148. memcpy(mcelog.entry + entry, mce, sizeof(struct mce));
  149. wmb();
  150. mcelog.entry[entry].finished = 1;
  151. wmb();
  152. mce->finished = 1;
  153. set_bit(0, &notify_user);
  154. }
  155. static void print_mce(struct mce *m, int *first)
  156. {
  157. if (*first) {
  158. printk(KERN_EMERG "\n" KERN_EMERG "HARDWARE ERROR\n");
  159. *first = 0;
  160. }
  161. printk(KERN_EMERG
  162. "CPU %d: Machine Check Exception: %16Lx Bank %d: %016Lx\n",
  163. m->extcpu, m->mcgstatus, m->bank, m->status);
  164. if (m->ip) {
  165. printk(KERN_EMERG "RIP%s %02x:<%016Lx> ",
  166. !(m->mcgstatus & MCG_STATUS_EIPV) ? " !INEXACT!" : "",
  167. m->cs, m->ip);
  168. if (m->cs == __KERNEL_CS)
  169. print_symbol("{%s}", m->ip);
  170. printk("\n");
  171. }
  172. printk(KERN_EMERG "TSC %llx ", m->tsc);
  173. if (m->addr)
  174. printk("ADDR %llx ", m->addr);
  175. if (m->misc)
  176. printk("MISC %llx ", m->misc);
  177. printk("\n");
  178. printk(KERN_EMERG "PROCESSOR %u:%x TIME %llu SOCKET %u APIC %x\n",
  179. m->cpuvendor, m->cpuid, m->time, m->socketid,
  180. m->apicid);
  181. }
  182. static void print_mce_tail(void)
  183. {
  184. printk(KERN_EMERG "This is not a software problem!\n"
  185. KERN_EMERG "Run through mcelog --ascii to decode and contact your hardware vendor\n");
  186. }
  187. #define PANIC_TIMEOUT 5 /* 5 seconds */
  188. static atomic_t mce_paniced;
  189. /* Panic in progress. Enable interrupts and wait for final IPI */
  190. static void wait_for_panic(void)
  191. {
  192. long timeout = PANIC_TIMEOUT*USEC_PER_SEC;
  193. preempt_disable();
  194. local_irq_enable();
  195. while (timeout-- > 0)
  196. udelay(1);
  197. if (panic_timeout == 0)
  198. panic_timeout = mce_panic_timeout;
  199. panic("Panicing machine check CPU died");
  200. }
  201. static void mce_panic(char *msg, struct mce *final, char *exp)
  202. {
  203. int i;
  204. int first = 1;
  205. /*
  206. * Make sure only one CPU runs in machine check panic
  207. */
  208. if (atomic_add_return(1, &mce_paniced) > 1)
  209. wait_for_panic();
  210. barrier();
  211. bust_spinlocks(1);
  212. console_verbose();
  213. /* First print corrected ones that are still unlogged */
  214. for (i = 0; i < MCE_LOG_LEN; i++) {
  215. struct mce *m = &mcelog.entry[i];
  216. if ((m->status & MCI_STATUS_VAL) &&
  217. !(m->status & MCI_STATUS_UC))
  218. print_mce(m, &first);
  219. }
  220. /* Now print uncorrected but with the final one last */
  221. for (i = 0; i < MCE_LOG_LEN; i++) {
  222. struct mce *m = &mcelog.entry[i];
  223. if (!(m->status & MCI_STATUS_VAL))
  224. continue;
  225. if (!final || memcmp(m, final, sizeof(struct mce)))
  226. print_mce(m, &first);
  227. }
  228. if (final)
  229. print_mce(final, &first);
  230. if (cpu_missing)
  231. printk(KERN_EMERG "Some CPUs didn't answer in synchronization\n");
  232. print_mce_tail();
  233. if (exp)
  234. printk(KERN_EMERG "Machine check: %s\n", exp);
  235. if (panic_timeout == 0)
  236. panic_timeout = mce_panic_timeout;
  237. panic(msg);
  238. }
  239. /* Support code for software error injection */
  240. static int msr_to_offset(u32 msr)
  241. {
  242. unsigned bank = __get_cpu_var(injectm.bank);
  243. if (msr == rip_msr)
  244. return offsetof(struct mce, ip);
  245. if (msr == MSR_IA32_MC0_STATUS + bank*4)
  246. return offsetof(struct mce, status);
  247. if (msr == MSR_IA32_MC0_ADDR + bank*4)
  248. return offsetof(struct mce, addr);
  249. if (msr == MSR_IA32_MC0_MISC + bank*4)
  250. return offsetof(struct mce, misc);
  251. if (msr == MSR_IA32_MCG_STATUS)
  252. return offsetof(struct mce, mcgstatus);
  253. return -1;
  254. }
  255. /* MSR access wrappers used for error injection */
  256. static u64 mce_rdmsrl(u32 msr)
  257. {
  258. u64 v;
  259. if (__get_cpu_var(injectm).finished) {
  260. int offset = msr_to_offset(msr);
  261. if (offset < 0)
  262. return 0;
  263. return *(u64 *)((char *)&__get_cpu_var(injectm) + offset);
  264. }
  265. rdmsrl(msr, v);
  266. return v;
  267. }
  268. static void mce_wrmsrl(u32 msr, u64 v)
  269. {
  270. if (__get_cpu_var(injectm).finished) {
  271. int offset = msr_to_offset(msr);
  272. if (offset >= 0)
  273. *(u64 *)((char *)&__get_cpu_var(injectm) + offset) = v;
  274. return;
  275. }
  276. wrmsrl(msr, v);
  277. }
  278. int mce_available(struct cpuinfo_x86 *c)
  279. {
  280. if (mce_disabled)
  281. return 0;
  282. return cpu_has(c, X86_FEATURE_MCE) && cpu_has(c, X86_FEATURE_MCA);
  283. }
  284. /*
  285. * Get the address of the instruction at the time of the machine check
  286. * error.
  287. */
  288. static inline void mce_get_rip(struct mce *m, struct pt_regs *regs)
  289. {
  290. if (regs && (m->mcgstatus & (MCG_STATUS_RIPV|MCG_STATUS_EIPV))) {
  291. m->ip = regs->ip;
  292. m->cs = regs->cs;
  293. } else {
  294. m->ip = 0;
  295. m->cs = 0;
  296. }
  297. if (rip_msr)
  298. m->ip = mce_rdmsrl(rip_msr);
  299. }
  300. #ifdef CONFIG_X86_LOCAL_APIC
  301. /*
  302. * Called after interrupts have been reenabled again
  303. * when a MCE happened during an interrupts off region
  304. * in the kernel.
  305. */
  306. asmlinkage void smp_mce_self_interrupt(struct pt_regs *regs)
  307. {
  308. ack_APIC_irq();
  309. exit_idle();
  310. irq_enter();
  311. mce_notify_irq();
  312. irq_exit();
  313. }
  314. #endif
  315. static void mce_report_event(struct pt_regs *regs)
  316. {
  317. if (regs->flags & (X86_VM_MASK|X86_EFLAGS_IF)) {
  318. mce_notify_irq();
  319. return;
  320. }
  321. #ifdef CONFIG_X86_LOCAL_APIC
  322. /*
  323. * Without APIC do not notify. The event will be picked
  324. * up eventually.
  325. */
  326. if (!cpu_has_apic)
  327. return;
  328. /*
  329. * When interrupts are disabled we cannot use
  330. * kernel services safely. Trigger an self interrupt
  331. * through the APIC to instead do the notification
  332. * after interrupts are reenabled again.
  333. */
  334. apic->send_IPI_self(MCE_SELF_VECTOR);
  335. /*
  336. * Wait for idle afterwards again so that we don't leave the
  337. * APIC in a non idle state because the normal APIC writes
  338. * cannot exclude us.
  339. */
  340. apic_wait_icr_idle();
  341. #endif
  342. }
  343. DEFINE_PER_CPU(unsigned, mce_poll_count);
  344. /*
  345. * Poll for corrected events or events that happened before reset.
  346. * Those are just logged through /dev/mcelog.
  347. *
  348. * This is executed in standard interrupt context.
  349. *
  350. * Note: spec recommends to panic for fatal unsignalled
  351. * errors here. However this would be quite problematic --
  352. * we would need to reimplement the Monarch handling and
  353. * it would mess up the exclusion between exception handler
  354. * and poll hander -- * so we skip this for now.
  355. * These cases should not happen anyways, or only when the CPU
  356. * is already totally * confused. In this case it's likely it will
  357. * not fully execute the machine check handler either.
  358. */
  359. void machine_check_poll(enum mcp_flags flags, mce_banks_t *b)
  360. {
  361. struct mce m;
  362. int i;
  363. __get_cpu_var(mce_poll_count)++;
  364. mce_setup(&m);
  365. m.mcgstatus = mce_rdmsrl(MSR_IA32_MCG_STATUS);
  366. for (i = 0; i < banks; i++) {
  367. if (!bank[i] || !test_bit(i, *b))
  368. continue;
  369. m.misc = 0;
  370. m.addr = 0;
  371. m.bank = i;
  372. m.tsc = 0;
  373. barrier();
  374. m.status = mce_rdmsrl(MSR_IA32_MC0_STATUS + i*4);
  375. if (!(m.status & MCI_STATUS_VAL))
  376. continue;
  377. /*
  378. * Uncorrected or signalled events are handled by the exception
  379. * handler when it is enabled, so don't process those here.
  380. *
  381. * TBD do the same check for MCI_STATUS_EN here?
  382. */
  383. if (!(flags & MCP_UC) &&
  384. (m.status & (mce_ser ? MCI_STATUS_S : MCI_STATUS_UC)))
  385. continue;
  386. if (m.status & MCI_STATUS_MISCV)
  387. m.misc = mce_rdmsrl(MSR_IA32_MC0_MISC + i*4);
  388. if (m.status & MCI_STATUS_ADDRV)
  389. m.addr = mce_rdmsrl(MSR_IA32_MC0_ADDR + i*4);
  390. if (!(flags & MCP_TIMESTAMP))
  391. m.tsc = 0;
  392. /*
  393. * Don't get the IP here because it's unlikely to
  394. * have anything to do with the actual error location.
  395. */
  396. if (!(flags & MCP_DONTLOG)) {
  397. mce_log(&m);
  398. add_taint(TAINT_MACHINE_CHECK);
  399. }
  400. /*
  401. * Clear state for this bank.
  402. */
  403. mce_wrmsrl(MSR_IA32_MC0_STATUS+4*i, 0);
  404. }
  405. /*
  406. * Don't clear MCG_STATUS here because it's only defined for
  407. * exceptions.
  408. */
  409. sync_core();
  410. }
  411. EXPORT_SYMBOL_GPL(machine_check_poll);
  412. /*
  413. * Do a quick check if any of the events requires a panic.
  414. * This decides if we keep the events around or clear them.
  415. */
  416. static int mce_no_way_out(struct mce *m, char **msg)
  417. {
  418. int i;
  419. for (i = 0; i < banks; i++) {
  420. m->status = mce_rdmsrl(MSR_IA32_MC0_STATUS + i*4);
  421. if (mce_severity(m, tolerant, msg) >= MCE_PANIC_SEVERITY)
  422. return 1;
  423. }
  424. return 0;
  425. }
  426. /*
  427. * Variable to establish order between CPUs while scanning.
  428. * Each CPU spins initially until executing is equal its number.
  429. */
  430. static atomic_t mce_executing;
  431. /*
  432. * Defines order of CPUs on entry. First CPU becomes Monarch.
  433. */
  434. static atomic_t mce_callin;
  435. /*
  436. * Check if a timeout waiting for other CPUs happened.
  437. */
  438. static int mce_timed_out(u64 *t)
  439. {
  440. /*
  441. * The others already did panic for some reason.
  442. * Bail out like in a timeout.
  443. * rmb() to tell the compiler that system_state
  444. * might have been modified by someone else.
  445. */
  446. rmb();
  447. if (atomic_read(&mce_paniced))
  448. wait_for_panic();
  449. if (!monarch_timeout)
  450. goto out;
  451. if ((s64)*t < SPINUNIT) {
  452. /* CHECKME: Make panic default for 1 too? */
  453. if (tolerant < 1)
  454. mce_panic("Timeout synchronizing machine check over CPUs",
  455. NULL, NULL);
  456. cpu_missing = 1;
  457. return 1;
  458. }
  459. *t -= SPINUNIT;
  460. out:
  461. touch_nmi_watchdog();
  462. return 0;
  463. }
  464. /*
  465. * The Monarch's reign. The Monarch is the CPU who entered
  466. * the machine check handler first. It waits for the others to
  467. * raise the exception too and then grades them. When any
  468. * error is fatal panic. Only then let the others continue.
  469. *
  470. * The other CPUs entering the MCE handler will be controlled by the
  471. * Monarch. They are called Subjects.
  472. *
  473. * This way we prevent any potential data corruption in a unrecoverable case
  474. * and also makes sure always all CPU's errors are examined.
  475. *
  476. * Also this detects the case of an machine check event coming from outer
  477. * space (not detected by any CPUs) In this case some external agent wants
  478. * us to shut down, so panic too.
  479. *
  480. * The other CPUs might still decide to panic if the handler happens
  481. * in a unrecoverable place, but in this case the system is in a semi-stable
  482. * state and won't corrupt anything by itself. It's ok to let the others
  483. * continue for a bit first.
  484. *
  485. * All the spin loops have timeouts; when a timeout happens a CPU
  486. * typically elects itself to be Monarch.
  487. */
  488. static void mce_reign(void)
  489. {
  490. int cpu;
  491. struct mce *m = NULL;
  492. int global_worst = 0;
  493. char *msg = NULL;
  494. char *nmsg = NULL;
  495. /*
  496. * This CPU is the Monarch and the other CPUs have run
  497. * through their handlers.
  498. * Grade the severity of the errors of all the CPUs.
  499. */
  500. for_each_possible_cpu(cpu) {
  501. int severity = mce_severity(&per_cpu(mces_seen, cpu), tolerant,
  502. &nmsg);
  503. if (severity > global_worst) {
  504. msg = nmsg;
  505. global_worst = severity;
  506. m = &per_cpu(mces_seen, cpu);
  507. }
  508. }
  509. /*
  510. * Cannot recover? Panic here then.
  511. * This dumps all the mces in the log buffer and stops the
  512. * other CPUs.
  513. */
  514. if (m && global_worst >= MCE_PANIC_SEVERITY && tolerant < 3)
  515. mce_panic("Fatal Machine check", m, msg);
  516. /*
  517. * For UC somewhere we let the CPU who detects it handle it.
  518. * Also must let continue the others, otherwise the handling
  519. * CPU could deadlock on a lock.
  520. */
  521. /*
  522. * No machine check event found. Must be some external
  523. * source or one CPU is hung. Panic.
  524. */
  525. if (!m && tolerant < 3)
  526. mce_panic("Machine check from unknown source", NULL, NULL);
  527. /*
  528. * Now clear all the mces_seen so that they don't reappear on
  529. * the next mce.
  530. */
  531. for_each_possible_cpu(cpu)
  532. memset(&per_cpu(mces_seen, cpu), 0, sizeof(struct mce));
  533. }
  534. static atomic_t global_nwo;
  535. /*
  536. * Start of Monarch synchronization. This waits until all CPUs have
  537. * entered the exception handler and then determines if any of them
  538. * saw a fatal event that requires panic. Then it executes them
  539. * in the entry order.
  540. * TBD double check parallel CPU hotunplug
  541. */
  542. static int mce_start(int no_way_out, int *order)
  543. {
  544. int nwo;
  545. int cpus = num_online_cpus();
  546. u64 timeout = (u64)monarch_timeout * NSEC_PER_USEC;
  547. if (!timeout) {
  548. *order = -1;
  549. return no_way_out;
  550. }
  551. atomic_add(no_way_out, &global_nwo);
  552. /*
  553. * Wait for everyone.
  554. */
  555. while (atomic_read(&mce_callin) != cpus) {
  556. if (mce_timed_out(&timeout)) {
  557. atomic_set(&global_nwo, 0);
  558. *order = -1;
  559. return no_way_out;
  560. }
  561. ndelay(SPINUNIT);
  562. }
  563. /*
  564. * Cache the global no_way_out state.
  565. */
  566. nwo = atomic_read(&global_nwo);
  567. /*
  568. * Monarch starts executing now, the others wait.
  569. */
  570. if (*order == 1) {
  571. atomic_set(&mce_executing, 1);
  572. return nwo;
  573. }
  574. /*
  575. * Now start the scanning loop one by one
  576. * in the original callin order.
  577. * This way when there are any shared banks it will
  578. * be only seen by one CPU before cleared, avoiding duplicates.
  579. */
  580. while (atomic_read(&mce_executing) < *order) {
  581. if (mce_timed_out(&timeout)) {
  582. atomic_set(&global_nwo, 0);
  583. *order = -1;
  584. return no_way_out;
  585. }
  586. ndelay(SPINUNIT);
  587. }
  588. return nwo;
  589. }
  590. /*
  591. * Synchronize between CPUs after main scanning loop.
  592. * This invokes the bulk of the Monarch processing.
  593. */
  594. static int mce_end(int order)
  595. {
  596. int ret = -1;
  597. u64 timeout = (u64)monarch_timeout * NSEC_PER_USEC;
  598. if (!timeout)
  599. goto reset;
  600. if (order < 0)
  601. goto reset;
  602. /*
  603. * Allow others to run.
  604. */
  605. atomic_inc(&mce_executing);
  606. if (order == 1) {
  607. /* CHECKME: Can this race with a parallel hotplug? */
  608. int cpus = num_online_cpus();
  609. /*
  610. * Monarch: Wait for everyone to go through their scanning
  611. * loops.
  612. */
  613. while (atomic_read(&mce_executing) <= cpus) {
  614. if (mce_timed_out(&timeout))
  615. goto reset;
  616. ndelay(SPINUNIT);
  617. }
  618. mce_reign();
  619. barrier();
  620. ret = 0;
  621. } else {
  622. /*
  623. * Subject: Wait for Monarch to finish.
  624. */
  625. while (atomic_read(&mce_executing) != 0) {
  626. if (mce_timed_out(&timeout))
  627. goto reset;
  628. ndelay(SPINUNIT);
  629. }
  630. /*
  631. * Don't reset anything. That's done by the Monarch.
  632. */
  633. return 0;
  634. }
  635. /*
  636. * Reset all global state.
  637. */
  638. reset:
  639. atomic_set(&global_nwo, 0);
  640. atomic_set(&mce_callin, 0);
  641. barrier();
  642. /*
  643. * Let others run again.
  644. */
  645. atomic_set(&mce_executing, 0);
  646. return ret;
  647. }
  648. static void mce_clear_state(unsigned long *toclear)
  649. {
  650. int i;
  651. for (i = 0; i < banks; i++) {
  652. if (test_bit(i, toclear))
  653. mce_wrmsrl(MSR_IA32_MC0_STATUS+4*i, 0);
  654. }
  655. }
  656. /*
  657. * The actual machine check handler. This only handles real
  658. * exceptions when something got corrupted coming in through int 18.
  659. *
  660. * This is executed in NMI context not subject to normal locking rules. This
  661. * implies that most kernel services cannot be safely used. Don't even
  662. * think about putting a printk in there!
  663. *
  664. * On Intel systems this is entered on all CPUs in parallel through
  665. * MCE broadcast. However some CPUs might be broken beyond repair,
  666. * so be always careful when synchronizing with others.
  667. */
  668. void do_machine_check(struct pt_regs *regs, long error_code)
  669. {
  670. struct mce m, *final;
  671. int i;
  672. int worst = 0;
  673. int severity;
  674. /*
  675. * Establish sequential order between the CPUs entering the machine
  676. * check handler.
  677. */
  678. int order;
  679. /*
  680. * If no_way_out gets set, there is no safe way to recover from this
  681. * MCE. If tolerant is cranked up, we'll try anyway.
  682. */
  683. int no_way_out = 0;
  684. /*
  685. * If kill_it gets set, there might be a way to recover from this
  686. * error.
  687. */
  688. int kill_it = 0;
  689. DECLARE_BITMAP(toclear, MAX_NR_BANKS);
  690. char *msg = "Unknown";
  691. atomic_inc(&mce_entry);
  692. __get_cpu_var(mce_exception_count)++;
  693. if (notify_die(DIE_NMI, "machine check", regs, error_code,
  694. 18, SIGKILL) == NOTIFY_STOP)
  695. goto out;
  696. if (!banks)
  697. goto out;
  698. order = atomic_add_return(1, &mce_callin);
  699. mce_setup(&m);
  700. m.mcgstatus = mce_rdmsrl(MSR_IA32_MCG_STATUS);
  701. no_way_out = mce_no_way_out(&m, &msg);
  702. final = &__get_cpu_var(mces_seen);
  703. *final = m;
  704. barrier();
  705. /*
  706. * When no restart IP must always kill or panic.
  707. */
  708. if (!(m.mcgstatus & MCG_STATUS_RIPV))
  709. kill_it = 1;
  710. /*
  711. * Go through all the banks in exclusion of the other CPUs.
  712. * This way we don't report duplicated events on shared banks
  713. * because the first one to see it will clear it.
  714. */
  715. no_way_out = mce_start(no_way_out, &order);
  716. for (i = 0; i < banks; i++) {
  717. __clear_bit(i, toclear);
  718. if (!bank[i])
  719. continue;
  720. m.misc = 0;
  721. m.addr = 0;
  722. m.bank = i;
  723. m.status = mce_rdmsrl(MSR_IA32_MC0_STATUS + i*4);
  724. if ((m.status & MCI_STATUS_VAL) == 0)
  725. continue;
  726. /*
  727. * Non uncorrected or non signaled errors are handled by
  728. * machine_check_poll. Leave them alone, unless this panics.
  729. */
  730. if (!(m.status & (mce_ser ? MCI_STATUS_S : MCI_STATUS_UC)) &&
  731. !no_way_out)
  732. continue;
  733. /*
  734. * Set taint even when machine check was not enabled.
  735. */
  736. add_taint(TAINT_MACHINE_CHECK);
  737. severity = mce_severity(&m, tolerant, NULL);
  738. /*
  739. * When machine check was for corrected handler don't touch,
  740. * unless we're panicing.
  741. */
  742. if (severity == MCE_KEEP_SEVERITY && !no_way_out)
  743. continue;
  744. __set_bit(i, toclear);
  745. if (severity == MCE_NO_SEVERITY) {
  746. /*
  747. * Machine check event was not enabled. Clear, but
  748. * ignore.
  749. */
  750. continue;
  751. }
  752. /*
  753. * Kill on action required.
  754. */
  755. if (severity == MCE_AR_SEVERITY)
  756. kill_it = 1;
  757. if (m.status & MCI_STATUS_MISCV)
  758. m.misc = mce_rdmsrl(MSR_IA32_MC0_MISC + i*4);
  759. if (m.status & MCI_STATUS_ADDRV)
  760. m.addr = mce_rdmsrl(MSR_IA32_MC0_ADDR + i*4);
  761. mce_get_rip(&m, regs);
  762. mce_log(&m);
  763. if (severity > worst) {
  764. *final = m;
  765. worst = severity;
  766. }
  767. }
  768. if (!no_way_out)
  769. mce_clear_state(toclear);
  770. /*
  771. * Do most of the synchronization with other CPUs.
  772. * When there's any problem use only local no_way_out state.
  773. */
  774. if (mce_end(order) < 0)
  775. no_way_out = worst >= MCE_PANIC_SEVERITY;
  776. /*
  777. * If we have decided that we just CAN'T continue, and the user
  778. * has not set tolerant to an insane level, give up and die.
  779. *
  780. * This is mainly used in the case when the system doesn't
  781. * support MCE broadcasting or it has been disabled.
  782. */
  783. if (no_way_out && tolerant < 3)
  784. mce_panic("Fatal machine check on current CPU", final, msg);
  785. /*
  786. * If the error seems to be unrecoverable, something should be
  787. * done. Try to kill as little as possible. If we can kill just
  788. * one task, do that. If the user has set the tolerance very
  789. * high, don't try to do anything at all.
  790. */
  791. if (kill_it && tolerant < 3)
  792. force_sig(SIGBUS, current);
  793. /* notify userspace ASAP */
  794. set_thread_flag(TIF_MCE_NOTIFY);
  795. if (worst > 0)
  796. mce_report_event(regs);
  797. mce_wrmsrl(MSR_IA32_MCG_STATUS, 0);
  798. out:
  799. atomic_dec(&mce_entry);
  800. sync_core();
  801. }
  802. EXPORT_SYMBOL_GPL(do_machine_check);
  803. #ifdef CONFIG_X86_MCE_INTEL
  804. /***
  805. * mce_log_therm_throt_event - Logs the thermal throttling event to mcelog
  806. * @cpu: The CPU on which the event occurred.
  807. * @status: Event status information
  808. *
  809. * This function should be called by the thermal interrupt after the
  810. * event has been processed and the decision was made to log the event
  811. * further.
  812. *
  813. * The status parameter will be saved to the 'status' field of 'struct mce'
  814. * and historically has been the register value of the
  815. * MSR_IA32_THERMAL_STATUS (Intel) msr.
  816. */
  817. void mce_log_therm_throt_event(__u64 status)
  818. {
  819. struct mce m;
  820. mce_setup(&m);
  821. m.bank = MCE_THERMAL_BANK;
  822. m.status = status;
  823. mce_log(&m);
  824. }
  825. #endif /* CONFIG_X86_MCE_INTEL */
  826. /*
  827. * Periodic polling timer for "silent" machine check errors. If the
  828. * poller finds an MCE, poll 2x faster. When the poller finds no more
  829. * errors, poll 2x slower (up to check_interval seconds).
  830. */
  831. static int check_interval = 5 * 60; /* 5 minutes */
  832. static DEFINE_PER_CPU(int, next_interval); /* in jiffies */
  833. static DEFINE_PER_CPU(struct timer_list, mce_timer);
  834. static void mcheck_timer(unsigned long data)
  835. {
  836. struct timer_list *t = &per_cpu(mce_timer, data);
  837. int *n;
  838. WARN_ON(smp_processor_id() != data);
  839. if (mce_available(&current_cpu_data)) {
  840. machine_check_poll(MCP_TIMESTAMP,
  841. &__get_cpu_var(mce_poll_banks));
  842. }
  843. /*
  844. * Alert userspace if needed. If we logged an MCE, reduce the
  845. * polling interval, otherwise increase the polling interval.
  846. */
  847. n = &__get_cpu_var(next_interval);
  848. if (mce_notify_irq())
  849. *n = max(*n/2, HZ/100);
  850. else
  851. *n = min(*n*2, (int)round_jiffies_relative(check_interval*HZ));
  852. t->expires = jiffies + *n;
  853. add_timer(t);
  854. }
  855. static void mce_do_trigger(struct work_struct *work)
  856. {
  857. call_usermodehelper(trigger, trigger_argv, NULL, UMH_NO_WAIT);
  858. }
  859. static DECLARE_WORK(mce_trigger_work, mce_do_trigger);
  860. /*
  861. * Notify the user(s) about new machine check events.
  862. * Can be called from interrupt context, but not from machine check/NMI
  863. * context.
  864. */
  865. int mce_notify_irq(void)
  866. {
  867. /* Not more than two messages every minute */
  868. static DEFINE_RATELIMIT_STATE(ratelimit, 60*HZ, 2);
  869. clear_thread_flag(TIF_MCE_NOTIFY);
  870. if (test_and_clear_bit(0, &notify_user)) {
  871. wake_up_interruptible(&mce_wait);
  872. /*
  873. * There is no risk of missing notifications because
  874. * work_pending is always cleared before the function is
  875. * executed.
  876. */
  877. if (trigger[0] && !work_pending(&mce_trigger_work))
  878. schedule_work(&mce_trigger_work);
  879. if (__ratelimit(&ratelimit))
  880. printk(KERN_INFO "Machine check events logged\n");
  881. return 1;
  882. }
  883. return 0;
  884. }
  885. EXPORT_SYMBOL_GPL(mce_notify_irq);
  886. /*
  887. * Initialize Machine Checks for a CPU.
  888. */
  889. static int mce_cap_init(void)
  890. {
  891. unsigned b;
  892. u64 cap;
  893. rdmsrl(MSR_IA32_MCG_CAP, cap);
  894. b = cap & MCG_BANKCNT_MASK;
  895. printk(KERN_INFO "mce: CPU supports %d MCE banks\n", b);
  896. if (b > MAX_NR_BANKS) {
  897. printk(KERN_WARNING
  898. "MCE: Using only %u machine check banks out of %u\n",
  899. MAX_NR_BANKS, b);
  900. b = MAX_NR_BANKS;
  901. }
  902. /* Don't support asymmetric configurations today */
  903. WARN_ON(banks != 0 && b != banks);
  904. banks = b;
  905. if (!bank) {
  906. bank = kmalloc(banks * sizeof(u64), GFP_KERNEL);
  907. if (!bank)
  908. return -ENOMEM;
  909. memset(bank, 0xff, banks * sizeof(u64));
  910. }
  911. /* Use accurate RIP reporting if available. */
  912. if ((cap & MCG_EXT_P) && MCG_EXT_CNT(cap) >= 9)
  913. rip_msr = MSR_IA32_MCG_EIP;
  914. if (cap & MCG_SER_P)
  915. mce_ser = 1;
  916. return 0;
  917. }
  918. static void mce_init(void)
  919. {
  920. mce_banks_t all_banks;
  921. u64 cap;
  922. int i;
  923. /*
  924. * Log the machine checks left over from the previous reset.
  925. */
  926. bitmap_fill(all_banks, MAX_NR_BANKS);
  927. machine_check_poll(MCP_UC|(!mce_bootlog ? MCP_DONTLOG : 0), &all_banks);
  928. set_in_cr4(X86_CR4_MCE);
  929. rdmsrl(MSR_IA32_MCG_CAP, cap);
  930. if (cap & MCG_CTL_P)
  931. wrmsr(MSR_IA32_MCG_CTL, 0xffffffff, 0xffffffff);
  932. for (i = 0; i < banks; i++) {
  933. if (skip_bank_init(i))
  934. continue;
  935. wrmsrl(MSR_IA32_MC0_CTL+4*i, bank[i]);
  936. wrmsrl(MSR_IA32_MC0_STATUS+4*i, 0);
  937. }
  938. }
  939. /* Add per CPU specific workarounds here */
  940. static void mce_cpu_quirks(struct cpuinfo_x86 *c)
  941. {
  942. /* This should be disabled by the BIOS, but isn't always */
  943. if (c->x86_vendor == X86_VENDOR_AMD) {
  944. if (c->x86 == 15 && banks > 4) {
  945. /*
  946. * disable GART TBL walk error reporting, which
  947. * trips off incorrectly with the IOMMU & 3ware
  948. * & Cerberus:
  949. */
  950. clear_bit(10, (unsigned long *)&bank[4]);
  951. }
  952. if (c->x86 <= 17 && mce_bootlog < 0) {
  953. /*
  954. * Lots of broken BIOS around that don't clear them
  955. * by default and leave crap in there. Don't log:
  956. */
  957. mce_bootlog = 0;
  958. }
  959. /*
  960. * Various K7s with broken bank 0 around. Always disable
  961. * by default.
  962. */
  963. if (c->x86 == 6)
  964. bank[0] = 0;
  965. }
  966. if (c->x86_vendor == X86_VENDOR_INTEL) {
  967. /*
  968. * SDM documents that on family 6 bank 0 should not be written
  969. * because it aliases to another special BIOS controlled
  970. * register.
  971. * But it's not aliased anymore on model 0x1a+
  972. * Don't ignore bank 0 completely because there could be a
  973. * valid event later, merely don't write CTL0.
  974. */
  975. if (c->x86 == 6 && c->x86_model < 0x1A)
  976. __set_bit(0, &dont_init_banks);
  977. /*
  978. * All newer Intel systems support MCE broadcasting. Enable
  979. * synchronization with a one second timeout.
  980. */
  981. if ((c->x86 > 6 || (c->x86 == 6 && c->x86_model >= 0xe)) &&
  982. monarch_timeout < 0)
  983. monarch_timeout = USEC_PER_SEC;
  984. }
  985. if (monarch_timeout < 0)
  986. monarch_timeout = 0;
  987. if (mce_bootlog != 0)
  988. mce_panic_timeout = 30;
  989. }
  990. static void __cpuinit mce_ancient_init(struct cpuinfo_x86 *c)
  991. {
  992. if (c->x86 != 5)
  993. return;
  994. switch (c->x86_vendor) {
  995. case X86_VENDOR_INTEL:
  996. if (mce_p5_enabled())
  997. intel_p5_mcheck_init(c);
  998. break;
  999. case X86_VENDOR_CENTAUR:
  1000. winchip_mcheck_init(c);
  1001. break;
  1002. }
  1003. }
  1004. static void mce_cpu_features(struct cpuinfo_x86 *c)
  1005. {
  1006. switch (c->x86_vendor) {
  1007. case X86_VENDOR_INTEL:
  1008. mce_intel_feature_init(c);
  1009. break;
  1010. case X86_VENDOR_AMD:
  1011. mce_amd_feature_init(c);
  1012. break;
  1013. default:
  1014. break;
  1015. }
  1016. }
  1017. static void mce_init_timer(void)
  1018. {
  1019. struct timer_list *t = &__get_cpu_var(mce_timer);
  1020. int *n = &__get_cpu_var(next_interval);
  1021. *n = check_interval * HZ;
  1022. if (!*n)
  1023. return;
  1024. setup_timer(t, mcheck_timer, smp_processor_id());
  1025. t->expires = round_jiffies(jiffies + *n);
  1026. add_timer(t);
  1027. }
  1028. /*
  1029. * Called for each booted CPU to set up machine checks.
  1030. * Must be called with preempt off:
  1031. */
  1032. void __cpuinit mcheck_init(struct cpuinfo_x86 *c)
  1033. {
  1034. if (mce_disabled)
  1035. return;
  1036. mce_ancient_init(c);
  1037. if (!mce_available(c))
  1038. return;
  1039. if (mce_cap_init() < 0) {
  1040. mce_disabled = 1;
  1041. return;
  1042. }
  1043. mce_cpu_quirks(c);
  1044. machine_check_vector = do_machine_check;
  1045. mce_init();
  1046. mce_cpu_features(c);
  1047. mce_init_timer();
  1048. }
  1049. /*
  1050. * Character device to read and clear the MCE log.
  1051. */
  1052. static DEFINE_SPINLOCK(mce_state_lock);
  1053. static int open_count; /* #times opened */
  1054. static int open_exclu; /* already open exclusive? */
  1055. static int mce_open(struct inode *inode, struct file *file)
  1056. {
  1057. spin_lock(&mce_state_lock);
  1058. if (open_exclu || (open_count && (file->f_flags & O_EXCL))) {
  1059. spin_unlock(&mce_state_lock);
  1060. return -EBUSY;
  1061. }
  1062. if (file->f_flags & O_EXCL)
  1063. open_exclu = 1;
  1064. open_count++;
  1065. spin_unlock(&mce_state_lock);
  1066. return nonseekable_open(inode, file);
  1067. }
  1068. static int mce_release(struct inode *inode, struct file *file)
  1069. {
  1070. spin_lock(&mce_state_lock);
  1071. open_count--;
  1072. open_exclu = 0;
  1073. spin_unlock(&mce_state_lock);
  1074. return 0;
  1075. }
  1076. static void collect_tscs(void *data)
  1077. {
  1078. unsigned long *cpu_tsc = (unsigned long *)data;
  1079. rdtscll(cpu_tsc[smp_processor_id()]);
  1080. }
  1081. static DEFINE_MUTEX(mce_read_mutex);
  1082. static ssize_t mce_read(struct file *filp, char __user *ubuf, size_t usize,
  1083. loff_t *off)
  1084. {
  1085. char __user *buf = ubuf;
  1086. unsigned long *cpu_tsc;
  1087. unsigned prev, next;
  1088. int i, err;
  1089. cpu_tsc = kmalloc(nr_cpu_ids * sizeof(long), GFP_KERNEL);
  1090. if (!cpu_tsc)
  1091. return -ENOMEM;
  1092. mutex_lock(&mce_read_mutex);
  1093. next = rcu_dereference(mcelog.next);
  1094. /* Only supports full reads right now */
  1095. if (*off != 0 || usize < MCE_LOG_LEN*sizeof(struct mce)) {
  1096. mutex_unlock(&mce_read_mutex);
  1097. kfree(cpu_tsc);
  1098. return -EINVAL;
  1099. }
  1100. err = 0;
  1101. prev = 0;
  1102. do {
  1103. for (i = prev; i < next; i++) {
  1104. unsigned long start = jiffies;
  1105. while (!mcelog.entry[i].finished) {
  1106. if (time_after_eq(jiffies, start + 2)) {
  1107. memset(mcelog.entry + i, 0,
  1108. sizeof(struct mce));
  1109. goto timeout;
  1110. }
  1111. cpu_relax();
  1112. }
  1113. smp_rmb();
  1114. err |= copy_to_user(buf, mcelog.entry + i,
  1115. sizeof(struct mce));
  1116. buf += sizeof(struct mce);
  1117. timeout:
  1118. ;
  1119. }
  1120. memset(mcelog.entry + prev, 0,
  1121. (next - prev) * sizeof(struct mce));
  1122. prev = next;
  1123. next = cmpxchg(&mcelog.next, prev, 0);
  1124. } while (next != prev);
  1125. synchronize_sched();
  1126. /*
  1127. * Collect entries that were still getting written before the
  1128. * synchronize.
  1129. */
  1130. on_each_cpu(collect_tscs, cpu_tsc, 1);
  1131. for (i = next; i < MCE_LOG_LEN; i++) {
  1132. if (mcelog.entry[i].finished &&
  1133. mcelog.entry[i].tsc < cpu_tsc[mcelog.entry[i].cpu]) {
  1134. err |= copy_to_user(buf, mcelog.entry+i,
  1135. sizeof(struct mce));
  1136. smp_rmb();
  1137. buf += sizeof(struct mce);
  1138. memset(&mcelog.entry[i], 0, sizeof(struct mce));
  1139. }
  1140. }
  1141. mutex_unlock(&mce_read_mutex);
  1142. kfree(cpu_tsc);
  1143. return err ? -EFAULT : buf - ubuf;
  1144. }
  1145. static unsigned int mce_poll(struct file *file, poll_table *wait)
  1146. {
  1147. poll_wait(file, &mce_wait, wait);
  1148. if (rcu_dereference(mcelog.next))
  1149. return POLLIN | POLLRDNORM;
  1150. return 0;
  1151. }
  1152. static long mce_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
  1153. {
  1154. int __user *p = (int __user *)arg;
  1155. if (!capable(CAP_SYS_ADMIN))
  1156. return -EPERM;
  1157. switch (cmd) {
  1158. case MCE_GET_RECORD_LEN:
  1159. return put_user(sizeof(struct mce), p);
  1160. case MCE_GET_LOG_LEN:
  1161. return put_user(MCE_LOG_LEN, p);
  1162. case MCE_GETCLEAR_FLAGS: {
  1163. unsigned flags;
  1164. do {
  1165. flags = mcelog.flags;
  1166. } while (cmpxchg(&mcelog.flags, flags, 0) != flags);
  1167. return put_user(flags, p);
  1168. }
  1169. default:
  1170. return -ENOTTY;
  1171. }
  1172. }
  1173. /* Modified in mce-inject.c, so not static or const */
  1174. struct file_operations mce_chrdev_ops = {
  1175. .open = mce_open,
  1176. .release = mce_release,
  1177. .read = mce_read,
  1178. .poll = mce_poll,
  1179. .unlocked_ioctl = mce_ioctl,
  1180. };
  1181. EXPORT_SYMBOL_GPL(mce_chrdev_ops);
  1182. static struct miscdevice mce_log_device = {
  1183. MISC_MCELOG_MINOR,
  1184. "mcelog",
  1185. &mce_chrdev_ops,
  1186. };
  1187. /*
  1188. * mce=off disables machine check
  1189. * mce=TOLERANCELEVEL[,monarchtimeout] (number, see above)
  1190. * monarchtimeout is how long to wait for other CPUs on machine
  1191. * check, or 0 to not wait
  1192. * mce=bootlog Log MCEs from before booting. Disabled by default on AMD.
  1193. * mce=nobootlog Don't log MCEs from before booting.
  1194. */
  1195. static int __init mcheck_enable(char *str)
  1196. {
  1197. if (*str == 0)
  1198. enable_p5_mce();
  1199. if (*str == '=')
  1200. str++;
  1201. if (!strcmp(str, "off"))
  1202. mce_disabled = 1;
  1203. else if (!strcmp(str, "bootlog") || !strcmp(str, "nobootlog"))
  1204. mce_bootlog = (str[0] == 'b');
  1205. else if (isdigit(str[0])) {
  1206. get_option(&str, &tolerant);
  1207. if (*str == ',') {
  1208. ++str;
  1209. get_option(&str, &monarch_timeout);
  1210. }
  1211. } else {
  1212. printk(KERN_INFO "mce argument %s ignored. Please use /sys\n",
  1213. str);
  1214. return 0;
  1215. }
  1216. return 1;
  1217. }
  1218. __setup("mce", mcheck_enable);
  1219. /*
  1220. * Sysfs support
  1221. */
  1222. /*
  1223. * Disable machine checks on suspend and shutdown. We can't really handle
  1224. * them later.
  1225. */
  1226. static int mce_disable(void)
  1227. {
  1228. int i;
  1229. for (i = 0; i < banks; i++) {
  1230. if (!skip_bank_init(i))
  1231. wrmsrl(MSR_IA32_MC0_CTL + i*4, 0);
  1232. }
  1233. return 0;
  1234. }
  1235. static int mce_suspend(struct sys_device *dev, pm_message_t state)
  1236. {
  1237. return mce_disable();
  1238. }
  1239. static int mce_shutdown(struct sys_device *dev)
  1240. {
  1241. return mce_disable();
  1242. }
  1243. /*
  1244. * On resume clear all MCE state. Don't want to see leftovers from the BIOS.
  1245. * Only one CPU is active at this time, the others get re-added later using
  1246. * CPU hotplug:
  1247. */
  1248. static int mce_resume(struct sys_device *dev)
  1249. {
  1250. mce_init();
  1251. mce_cpu_features(&current_cpu_data);
  1252. return 0;
  1253. }
  1254. static void mce_cpu_restart(void *data)
  1255. {
  1256. del_timer_sync(&__get_cpu_var(mce_timer));
  1257. if (mce_available(&current_cpu_data))
  1258. mce_init();
  1259. mce_init_timer();
  1260. }
  1261. /* Reinit MCEs after user configuration changes */
  1262. static void mce_restart(void)
  1263. {
  1264. on_each_cpu(mce_cpu_restart, NULL, 1);
  1265. }
  1266. static struct sysdev_class mce_sysclass = {
  1267. .suspend = mce_suspend,
  1268. .shutdown = mce_shutdown,
  1269. .resume = mce_resume,
  1270. .name = "machinecheck",
  1271. };
  1272. DEFINE_PER_CPU(struct sys_device, mce_dev);
  1273. __cpuinitdata
  1274. void (*threshold_cpu_callback)(unsigned long action, unsigned int cpu);
  1275. static struct sysdev_attribute *bank_attrs;
  1276. static ssize_t show_bank(struct sys_device *s, struct sysdev_attribute *attr,
  1277. char *buf)
  1278. {
  1279. u64 b = bank[attr - bank_attrs];
  1280. return sprintf(buf, "%llx\n", b);
  1281. }
  1282. static ssize_t set_bank(struct sys_device *s, struct sysdev_attribute *attr,
  1283. const char *buf, size_t size)
  1284. {
  1285. u64 new;
  1286. if (strict_strtoull(buf, 0, &new) < 0)
  1287. return -EINVAL;
  1288. bank[attr - bank_attrs] = new;
  1289. mce_restart();
  1290. return size;
  1291. }
  1292. static ssize_t
  1293. show_trigger(struct sys_device *s, struct sysdev_attribute *attr, char *buf)
  1294. {
  1295. strcpy(buf, trigger);
  1296. strcat(buf, "\n");
  1297. return strlen(trigger) + 1;
  1298. }
  1299. static ssize_t set_trigger(struct sys_device *s, struct sysdev_attribute *attr,
  1300. const char *buf, size_t siz)
  1301. {
  1302. char *p;
  1303. int len;
  1304. strncpy(trigger, buf, sizeof(trigger));
  1305. trigger[sizeof(trigger)-1] = 0;
  1306. len = strlen(trigger);
  1307. p = strchr(trigger, '\n');
  1308. if (*p)
  1309. *p = 0;
  1310. return len;
  1311. }
  1312. static ssize_t store_int_with_restart(struct sys_device *s,
  1313. struct sysdev_attribute *attr,
  1314. const char *buf, size_t size)
  1315. {
  1316. ssize_t ret = sysdev_store_int(s, attr, buf, size);
  1317. mce_restart();
  1318. return ret;
  1319. }
  1320. static SYSDEV_ATTR(trigger, 0644, show_trigger, set_trigger);
  1321. static SYSDEV_INT_ATTR(tolerant, 0644, tolerant);
  1322. static SYSDEV_INT_ATTR(monarch_timeout, 0644, monarch_timeout);
  1323. static struct sysdev_ext_attribute attr_check_interval = {
  1324. _SYSDEV_ATTR(check_interval, 0644, sysdev_show_int,
  1325. store_int_with_restart),
  1326. &check_interval
  1327. };
  1328. static struct sysdev_attribute *mce_attrs[] = {
  1329. &attr_tolerant.attr, &attr_check_interval.attr, &attr_trigger,
  1330. &attr_monarch_timeout.attr,
  1331. NULL
  1332. };
  1333. static cpumask_var_t mce_dev_initialized;
  1334. /* Per cpu sysdev init. All of the cpus still share the same ctrl bank: */
  1335. static __cpuinit int mce_create_device(unsigned int cpu)
  1336. {
  1337. int err;
  1338. int i;
  1339. if (!mce_available(&boot_cpu_data))
  1340. return -EIO;
  1341. memset(&per_cpu(mce_dev, cpu).kobj, 0, sizeof(struct kobject));
  1342. per_cpu(mce_dev, cpu).id = cpu;
  1343. per_cpu(mce_dev, cpu).cls = &mce_sysclass;
  1344. err = sysdev_register(&per_cpu(mce_dev, cpu));
  1345. if (err)
  1346. return err;
  1347. for (i = 0; mce_attrs[i]; i++) {
  1348. err = sysdev_create_file(&per_cpu(mce_dev, cpu), mce_attrs[i]);
  1349. if (err)
  1350. goto error;
  1351. }
  1352. for (i = 0; i < banks; i++) {
  1353. err = sysdev_create_file(&per_cpu(mce_dev, cpu),
  1354. &bank_attrs[i]);
  1355. if (err)
  1356. goto error2;
  1357. }
  1358. cpumask_set_cpu(cpu, mce_dev_initialized);
  1359. return 0;
  1360. error2:
  1361. while (--i >= 0)
  1362. sysdev_remove_file(&per_cpu(mce_dev, cpu), &bank_attrs[i]);
  1363. error:
  1364. while (--i >= 0)
  1365. sysdev_remove_file(&per_cpu(mce_dev, cpu), mce_attrs[i]);
  1366. sysdev_unregister(&per_cpu(mce_dev, cpu));
  1367. return err;
  1368. }
  1369. static __cpuinit void mce_remove_device(unsigned int cpu)
  1370. {
  1371. int i;
  1372. if (!cpumask_test_cpu(cpu, mce_dev_initialized))
  1373. return;
  1374. for (i = 0; mce_attrs[i]; i++)
  1375. sysdev_remove_file(&per_cpu(mce_dev, cpu), mce_attrs[i]);
  1376. for (i = 0; i < banks; i++)
  1377. sysdev_remove_file(&per_cpu(mce_dev, cpu), &bank_attrs[i]);
  1378. sysdev_unregister(&per_cpu(mce_dev, cpu));
  1379. cpumask_clear_cpu(cpu, mce_dev_initialized);
  1380. }
  1381. /* Make sure there are no machine checks on offlined CPUs. */
  1382. static void mce_disable_cpu(void *h)
  1383. {
  1384. unsigned long action = *(unsigned long *)h;
  1385. int i;
  1386. if (!mce_available(&current_cpu_data))
  1387. return;
  1388. if (!(action & CPU_TASKS_FROZEN))
  1389. cmci_clear();
  1390. for (i = 0; i < banks; i++) {
  1391. if (!skip_bank_init(i))
  1392. wrmsrl(MSR_IA32_MC0_CTL + i*4, 0);
  1393. }
  1394. }
  1395. static void mce_reenable_cpu(void *h)
  1396. {
  1397. unsigned long action = *(unsigned long *)h;
  1398. int i;
  1399. if (!mce_available(&current_cpu_data))
  1400. return;
  1401. if (!(action & CPU_TASKS_FROZEN))
  1402. cmci_reenable();
  1403. for (i = 0; i < banks; i++) {
  1404. if (!skip_bank_init(i))
  1405. wrmsrl(MSR_IA32_MC0_CTL + i*4, bank[i]);
  1406. }
  1407. }
  1408. /* Get notified when a cpu comes on/off. Be hotplug friendly. */
  1409. static int __cpuinit
  1410. mce_cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
  1411. {
  1412. unsigned int cpu = (unsigned long)hcpu;
  1413. struct timer_list *t = &per_cpu(mce_timer, cpu);
  1414. switch (action) {
  1415. case CPU_ONLINE:
  1416. case CPU_ONLINE_FROZEN:
  1417. mce_create_device(cpu);
  1418. if (threshold_cpu_callback)
  1419. threshold_cpu_callback(action, cpu);
  1420. break;
  1421. case CPU_DEAD:
  1422. case CPU_DEAD_FROZEN:
  1423. if (threshold_cpu_callback)
  1424. threshold_cpu_callback(action, cpu);
  1425. mce_remove_device(cpu);
  1426. break;
  1427. case CPU_DOWN_PREPARE:
  1428. case CPU_DOWN_PREPARE_FROZEN:
  1429. del_timer_sync(t);
  1430. smp_call_function_single(cpu, mce_disable_cpu, &action, 1);
  1431. break;
  1432. case CPU_DOWN_FAILED:
  1433. case CPU_DOWN_FAILED_FROZEN:
  1434. t->expires = round_jiffies(jiffies +
  1435. __get_cpu_var(next_interval));
  1436. add_timer_on(t, cpu);
  1437. smp_call_function_single(cpu, mce_reenable_cpu, &action, 1);
  1438. break;
  1439. case CPU_POST_DEAD:
  1440. /* intentionally ignoring frozen here */
  1441. cmci_rediscover(cpu);
  1442. break;
  1443. }
  1444. return NOTIFY_OK;
  1445. }
  1446. static struct notifier_block mce_cpu_notifier __cpuinitdata = {
  1447. .notifier_call = mce_cpu_callback,
  1448. };
  1449. static __init int mce_init_banks(void)
  1450. {
  1451. int i;
  1452. bank_attrs = kzalloc(sizeof(struct sysdev_attribute) * banks,
  1453. GFP_KERNEL);
  1454. if (!bank_attrs)
  1455. return -ENOMEM;
  1456. for (i = 0; i < banks; i++) {
  1457. struct sysdev_attribute *a = &bank_attrs[i];
  1458. a->attr.name = kasprintf(GFP_KERNEL, "bank%d", i);
  1459. if (!a->attr.name)
  1460. goto nomem;
  1461. a->attr.mode = 0644;
  1462. a->show = show_bank;
  1463. a->store = set_bank;
  1464. }
  1465. return 0;
  1466. nomem:
  1467. while (--i >= 0)
  1468. kfree(bank_attrs[i].attr.name);
  1469. kfree(bank_attrs);
  1470. bank_attrs = NULL;
  1471. return -ENOMEM;
  1472. }
  1473. static __init int mce_init_device(void)
  1474. {
  1475. int err;
  1476. int i = 0;
  1477. if (!mce_available(&boot_cpu_data))
  1478. return -EIO;
  1479. alloc_cpumask_var(&mce_dev_initialized, GFP_KERNEL);
  1480. err = mce_init_banks();
  1481. if (err)
  1482. return err;
  1483. err = sysdev_class_register(&mce_sysclass);
  1484. if (err)
  1485. return err;
  1486. for_each_online_cpu(i) {
  1487. err = mce_create_device(i);
  1488. if (err)
  1489. return err;
  1490. }
  1491. register_hotcpu_notifier(&mce_cpu_notifier);
  1492. misc_register(&mce_log_device);
  1493. return err;
  1494. }
  1495. device_initcall(mce_init_device);
  1496. #else /* CONFIG_X86_OLD_MCE: */
  1497. int nr_mce_banks;
  1498. EXPORT_SYMBOL_GPL(nr_mce_banks); /* non-fatal.o */
  1499. /* This has to be run for each processor */
  1500. void mcheck_init(struct cpuinfo_x86 *c)
  1501. {
  1502. if (mce_disabled == 1)
  1503. return;
  1504. switch (c->x86_vendor) {
  1505. case X86_VENDOR_AMD:
  1506. amd_mcheck_init(c);
  1507. break;
  1508. case X86_VENDOR_INTEL:
  1509. if (c->x86 == 5)
  1510. intel_p5_mcheck_init(c);
  1511. if (c->x86 == 6)
  1512. intel_p6_mcheck_init(c);
  1513. if (c->x86 == 15)
  1514. intel_p4_mcheck_init(c);
  1515. break;
  1516. case X86_VENDOR_CENTAUR:
  1517. if (c->x86 == 5)
  1518. winchip_mcheck_init(c);
  1519. break;
  1520. default:
  1521. break;
  1522. }
  1523. printk(KERN_INFO "mce: CPU supports %d MCE banks\n", nr_mce_banks);
  1524. }
  1525. static int __init mcheck_enable(char *str)
  1526. {
  1527. mce_disabled = -1;
  1528. return 1;
  1529. }
  1530. __setup("mce", mcheck_enable);
  1531. #endif /* CONFIG_X86_OLD_MCE */
  1532. /*
  1533. * Old style boot options parsing. Only for compatibility.
  1534. */
  1535. static int __init mcheck_disable(char *str)
  1536. {
  1537. mce_disabled = 1;
  1538. return 1;
  1539. }
  1540. __setup("nomce", mcheck_disable);