mce.c 48 KB

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