mce.c 48 KB

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