mce.c 48 KB

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