mce.c 49 KB

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