mce.c 49 KB

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