mce.c 50 KB

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