mce.c 48 KB

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