mce.c 47 KB

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