mce.c 48 KB

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