mce.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182
  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_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. pr_emerg(HW_ERR "PROCESSOR %u:%x TIME %llu SOCKET %u APIC %x\n",
  184. m->cpuvendor, m->cpuid, m->time, m->socketid, m->apicid);
  185. /*
  186. * Print out human-readable details about the MCE error,
  187. * (if the CPU has an implementation for that)
  188. */
  189. ret = atomic_notifier_call_chain(&x86_mce_decoder_chain, 0, m);
  190. if (ret == NOTIFY_STOP)
  191. return;
  192. pr_emerg_ratelimited(HW_ERR "Run the above through 'mcelog --ascii'\n");
  193. }
  194. #define PANIC_TIMEOUT 5 /* 5 seconds */
  195. static atomic_t mce_paniced;
  196. static int fake_panic;
  197. static atomic_t mce_fake_paniced;
  198. /* Panic in progress. Enable interrupts and wait for final IPI */
  199. static void wait_for_panic(void)
  200. {
  201. long timeout = PANIC_TIMEOUT*USEC_PER_SEC;
  202. preempt_disable();
  203. local_irq_enable();
  204. while (timeout-- > 0)
  205. udelay(1);
  206. if (panic_timeout == 0)
  207. panic_timeout = mce_panic_timeout;
  208. panic("Panicing machine check CPU died");
  209. }
  210. static void mce_panic(char *msg, struct mce *final, char *exp)
  211. {
  212. int i, apei_err = 0;
  213. if (!fake_panic) {
  214. /*
  215. * Make sure only one CPU runs in machine check panic
  216. */
  217. if (atomic_inc_return(&mce_paniced) > 1)
  218. wait_for_panic();
  219. barrier();
  220. bust_spinlocks(1);
  221. console_verbose();
  222. } else {
  223. /* Don't log too much for fake panic */
  224. if (atomic_inc_return(&mce_fake_paniced) > 1)
  225. return;
  226. }
  227. /* First print corrected ones that are still unlogged */
  228. for (i = 0; i < MCE_LOG_LEN; i++) {
  229. struct mce *m = &mcelog.entry[i];
  230. if (!(m->status & MCI_STATUS_VAL))
  231. continue;
  232. if (!(m->status & MCI_STATUS_UC)) {
  233. print_mce(m);
  234. if (!apei_err)
  235. apei_err = apei_write_mce(m);
  236. }
  237. }
  238. /* Now print uncorrected but with the final one last */
  239. for (i = 0; i < MCE_LOG_LEN; i++) {
  240. struct mce *m = &mcelog.entry[i];
  241. if (!(m->status & MCI_STATUS_VAL))
  242. continue;
  243. if (!(m->status & MCI_STATUS_UC))
  244. continue;
  245. if (!final || memcmp(m, final, sizeof(struct mce))) {
  246. print_mce(m);
  247. if (!apei_err)
  248. apei_err = apei_write_mce(m);
  249. }
  250. }
  251. if (final) {
  252. print_mce(final);
  253. if (!apei_err)
  254. apei_err = apei_write_mce(final);
  255. }
  256. if (cpu_missing)
  257. pr_emerg(HW_ERR "Some CPUs didn't answer in synchronization\n");
  258. if (exp)
  259. pr_emerg(HW_ERR "Machine check: %s\n", exp);
  260. if (!fake_panic) {
  261. if (panic_timeout == 0)
  262. panic_timeout = mce_panic_timeout;
  263. panic(msg);
  264. } else
  265. pr_emerg(HW_ERR "Fake kernel panic: %s\n", msg);
  266. }
  267. /* Support code for software error injection */
  268. static int msr_to_offset(u32 msr)
  269. {
  270. unsigned bank = __this_cpu_read(injectm.bank);
  271. if (msr == rip_msr)
  272. return offsetof(struct mce, ip);
  273. if (msr == MSR_IA32_MCx_STATUS(bank))
  274. return offsetof(struct mce, status);
  275. if (msr == MSR_IA32_MCx_ADDR(bank))
  276. return offsetof(struct mce, addr);
  277. if (msr == MSR_IA32_MCx_MISC(bank))
  278. return offsetof(struct mce, misc);
  279. if (msr == MSR_IA32_MCG_STATUS)
  280. return offsetof(struct mce, mcgstatus);
  281. return -1;
  282. }
  283. /* MSR access wrappers used for error injection */
  284. static u64 mce_rdmsrl(u32 msr)
  285. {
  286. u64 v;
  287. if (__this_cpu_read(injectm.finished)) {
  288. int offset = msr_to_offset(msr);
  289. if (offset < 0)
  290. return 0;
  291. return *(u64 *)((char *)&__get_cpu_var(injectm) + offset);
  292. }
  293. if (rdmsrl_safe(msr, &v)) {
  294. WARN_ONCE(1, "mce: Unable to read msr %d!\n", msr);
  295. /*
  296. * Return zero in case the access faulted. This should
  297. * not happen normally but can happen if the CPU does
  298. * something weird, or if the code is buggy.
  299. */
  300. v = 0;
  301. }
  302. return v;
  303. }
  304. static void mce_wrmsrl(u32 msr, u64 v)
  305. {
  306. if (__this_cpu_read(injectm.finished)) {
  307. int offset = msr_to_offset(msr);
  308. if (offset >= 0)
  309. *(u64 *)((char *)&__get_cpu_var(injectm) + offset) = v;
  310. return;
  311. }
  312. wrmsrl(msr, v);
  313. }
  314. /*
  315. * Collect all global (w.r.t. this processor) status about this machine
  316. * check into our "mce" struct so that we can use it later to assess
  317. * the severity of the problem as we read per-bank specific details.
  318. */
  319. static inline void mce_gather_info(struct mce *m, struct pt_regs *regs)
  320. {
  321. mce_setup(m);
  322. m->mcgstatus = mce_rdmsrl(MSR_IA32_MCG_STATUS);
  323. if (regs) {
  324. /*
  325. * Get the address of the instruction at the time of
  326. * the machine check error.
  327. */
  328. if (m->mcgstatus & (MCG_STATUS_RIPV|MCG_STATUS_EIPV)) {
  329. m->ip = regs->ip;
  330. m->cs = regs->cs;
  331. }
  332. /* Use accurate RIP reporting if available. */
  333. if (rip_msr)
  334. m->ip = mce_rdmsrl(rip_msr);
  335. }
  336. }
  337. /*
  338. * Simple lockless ring to communicate PFNs from the exception handler with the
  339. * process context work function. This is vastly simplified because there's
  340. * only a single reader and a single writer.
  341. */
  342. #define MCE_RING_SIZE 16 /* we use one entry less */
  343. struct mce_ring {
  344. unsigned short start;
  345. unsigned short end;
  346. unsigned long ring[MCE_RING_SIZE];
  347. };
  348. static DEFINE_PER_CPU(struct mce_ring, mce_ring);
  349. /* Runs with CPU affinity in workqueue */
  350. static int mce_ring_empty(void)
  351. {
  352. struct mce_ring *r = &__get_cpu_var(mce_ring);
  353. return r->start == r->end;
  354. }
  355. static int mce_ring_get(unsigned long *pfn)
  356. {
  357. struct mce_ring *r;
  358. int ret = 0;
  359. *pfn = 0;
  360. get_cpu();
  361. r = &__get_cpu_var(mce_ring);
  362. if (r->start == r->end)
  363. goto out;
  364. *pfn = r->ring[r->start];
  365. r->start = (r->start + 1) % MCE_RING_SIZE;
  366. ret = 1;
  367. out:
  368. put_cpu();
  369. return ret;
  370. }
  371. /* Always runs in MCE context with preempt off */
  372. static int mce_ring_add(unsigned long pfn)
  373. {
  374. struct mce_ring *r = &__get_cpu_var(mce_ring);
  375. unsigned next;
  376. next = (r->end + 1) % MCE_RING_SIZE;
  377. if (next == r->start)
  378. return -1;
  379. r->ring[r->end] = pfn;
  380. wmb();
  381. r->end = next;
  382. return 0;
  383. }
  384. int mce_available(struct cpuinfo_x86 *c)
  385. {
  386. if (mce_disabled)
  387. return 0;
  388. return cpu_has(c, X86_FEATURE_MCE) && cpu_has(c, X86_FEATURE_MCA);
  389. }
  390. static void mce_schedule_work(void)
  391. {
  392. if (!mce_ring_empty()) {
  393. struct work_struct *work = &__get_cpu_var(mce_work);
  394. if (!work_pending(work))
  395. schedule_work(work);
  396. }
  397. }
  398. DEFINE_PER_CPU(struct irq_work, mce_irq_work);
  399. static void mce_irq_work_cb(struct irq_work *entry)
  400. {
  401. mce_notify_irq();
  402. mce_schedule_work();
  403. }
  404. static void mce_report_event(struct pt_regs *regs)
  405. {
  406. if (regs->flags & (X86_VM_MASK|X86_EFLAGS_IF)) {
  407. mce_notify_irq();
  408. /*
  409. * Triggering the work queue here is just an insurance
  410. * policy in case the syscall exit notify handler
  411. * doesn't run soon enough or ends up running on the
  412. * wrong CPU (can happen when audit sleeps)
  413. */
  414. mce_schedule_work();
  415. return;
  416. }
  417. irq_work_queue(&__get_cpu_var(mce_irq_work));
  418. }
  419. DEFINE_PER_CPU(unsigned, mce_poll_count);
  420. /*
  421. * Poll for corrected events or events that happened before reset.
  422. * Those are just logged through /dev/mcelog.
  423. *
  424. * This is executed in standard interrupt context.
  425. *
  426. * Note: spec recommends to panic for fatal unsignalled
  427. * errors here. However this would be quite problematic --
  428. * we would need to reimplement the Monarch handling and
  429. * it would mess up the exclusion between exception handler
  430. * and poll hander -- * so we skip this for now.
  431. * These cases should not happen anyways, or only when the CPU
  432. * is already totally * confused. In this case it's likely it will
  433. * not fully execute the machine check handler either.
  434. */
  435. void machine_check_poll(enum mcp_flags flags, mce_banks_t *b)
  436. {
  437. struct mce m;
  438. int i;
  439. percpu_inc(mce_poll_count);
  440. mce_gather_info(&m, NULL);
  441. for (i = 0; i < banks; i++) {
  442. if (!mce_banks[i].ctl || !test_bit(i, *b))
  443. continue;
  444. m.misc = 0;
  445. m.addr = 0;
  446. m.bank = i;
  447. m.tsc = 0;
  448. barrier();
  449. m.status = mce_rdmsrl(MSR_IA32_MCx_STATUS(i));
  450. if (!(m.status & MCI_STATUS_VAL))
  451. continue;
  452. /*
  453. * Uncorrected or signalled events are handled by the exception
  454. * handler when it is enabled, so don't process those here.
  455. *
  456. * TBD do the same check for MCI_STATUS_EN here?
  457. */
  458. if (!(flags & MCP_UC) &&
  459. (m.status & (mce_ser ? MCI_STATUS_S : MCI_STATUS_UC)))
  460. continue;
  461. if (m.status & MCI_STATUS_MISCV)
  462. m.misc = mce_rdmsrl(MSR_IA32_MCx_MISC(i));
  463. if (m.status & MCI_STATUS_ADDRV)
  464. m.addr = mce_rdmsrl(MSR_IA32_MCx_ADDR(i));
  465. if (!(flags & MCP_TIMESTAMP))
  466. m.tsc = 0;
  467. /*
  468. * Don't get the IP here because it's unlikely to
  469. * have anything to do with the actual error location.
  470. */
  471. if (!(flags & MCP_DONTLOG) && !mce_dont_log_ce)
  472. mce_log(&m);
  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 (MCI_MISC_ADDR_LSB(m->misc) > PAGE_SHIFT)
  737. return 0;
  738. if (MCI_MISC_ADDR_MODE(m->misc) != MCI_MISC_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_gather_info(&m, regs);
  793. final = &__get_cpu_var(mces_seen);
  794. *final = m;
  795. no_way_out = mce_no_way_out(&m, &msg);
  796. barrier();
  797. /*
  798. * When no restart IP must always kill or panic.
  799. */
  800. if (!(m.mcgstatus & MCG_STATUS_RIPV))
  801. kill_it = 1;
  802. /*
  803. * Go through all the banks in exclusion of the other CPUs.
  804. * This way we don't report duplicated events on shared banks
  805. * because the first one to see it will clear it.
  806. */
  807. order = mce_start(&no_way_out);
  808. for (i = 0; i < banks; i++) {
  809. __clear_bit(i, toclear);
  810. if (!mce_banks[i].ctl)
  811. continue;
  812. m.misc = 0;
  813. m.addr = 0;
  814. m.bank = i;
  815. m.status = mce_rdmsrl(MSR_IA32_MCx_STATUS(i));
  816. if ((m.status & MCI_STATUS_VAL) == 0)
  817. continue;
  818. /*
  819. * Non uncorrected or non signaled errors are handled by
  820. * machine_check_poll. Leave them alone, unless this panics.
  821. */
  822. if (!(m.status & (mce_ser ? MCI_STATUS_S : MCI_STATUS_UC)) &&
  823. !no_way_out)
  824. continue;
  825. /*
  826. * Set taint even when machine check was not enabled.
  827. */
  828. add_taint(TAINT_MACHINE_CHECK);
  829. severity = mce_severity(&m, tolerant, NULL);
  830. /*
  831. * When machine check was for corrected handler don't touch,
  832. * unless we're panicing.
  833. */
  834. if (severity == MCE_KEEP_SEVERITY && !no_way_out)
  835. continue;
  836. __set_bit(i, toclear);
  837. if (severity == MCE_NO_SEVERITY) {
  838. /*
  839. * Machine check event was not enabled. Clear, but
  840. * ignore.
  841. */
  842. continue;
  843. }
  844. /*
  845. * Kill on action required.
  846. */
  847. if (severity == MCE_AR_SEVERITY)
  848. kill_it = 1;
  849. if (m.status & MCI_STATUS_MISCV)
  850. m.misc = mce_rdmsrl(MSR_IA32_MCx_MISC(i));
  851. if (m.status & MCI_STATUS_ADDRV)
  852. m.addr = mce_rdmsrl(MSR_IA32_MCx_ADDR(i));
  853. /*
  854. * Action optional error. Queue address for later processing.
  855. * When the ring overflows we just ignore the AO error.
  856. * RED-PEN add some logging mechanism when
  857. * usable_address or mce_add_ring fails.
  858. * RED-PEN don't ignore overflow for tolerant == 0
  859. */
  860. if (severity == MCE_AO_SEVERITY && mce_usable_address(&m))
  861. mce_ring_add(m.addr >> PAGE_SHIFT);
  862. mce_log(&m);
  863. if (severity > worst) {
  864. *final = m;
  865. worst = severity;
  866. }
  867. }
  868. if (!no_way_out)
  869. mce_clear_state(toclear);
  870. /*
  871. * Do most of the synchronization with other CPUs.
  872. * When there's any problem use only local no_way_out state.
  873. */
  874. if (mce_end(order) < 0)
  875. no_way_out = worst >= MCE_PANIC_SEVERITY;
  876. /*
  877. * If we have decided that we just CAN'T continue, and the user
  878. * has not set tolerant to an insane level, give up and die.
  879. *
  880. * This is mainly used in the case when the system doesn't
  881. * support MCE broadcasting or it has been disabled.
  882. */
  883. if (no_way_out && tolerant < 3)
  884. mce_panic("Fatal machine check on current CPU", final, msg);
  885. /*
  886. * If the error seems to be unrecoverable, something should be
  887. * done. Try to kill as little as possible. If we can kill just
  888. * one task, do that. If the user has set the tolerance very
  889. * high, don't try to do anything at all.
  890. */
  891. if (kill_it && tolerant < 3)
  892. force_sig(SIGBUS, current);
  893. /* notify userspace ASAP */
  894. set_thread_flag(TIF_MCE_NOTIFY);
  895. if (worst > 0)
  896. mce_report_event(regs);
  897. mce_wrmsrl(MSR_IA32_MCG_STATUS, 0);
  898. out:
  899. atomic_dec(&mce_entry);
  900. sync_core();
  901. }
  902. EXPORT_SYMBOL_GPL(do_machine_check);
  903. /* dummy to break dependency. actual code is in mm/memory-failure.c */
  904. void __attribute__((weak)) memory_failure(unsigned long pfn, int vector)
  905. {
  906. printk(KERN_ERR "Action optional memory failure at %lx ignored\n", pfn);
  907. }
  908. /*
  909. * Called after mce notification in process context. This code
  910. * is allowed to sleep. Call the high level VM handler to process
  911. * any corrupted pages.
  912. * Assume that the work queue code only calls this one at a time
  913. * per CPU.
  914. * Note we don't disable preemption, so this code might run on the wrong
  915. * CPU. In this case the event is picked up by the scheduled work queue.
  916. * This is merely a fast path to expedite processing in some common
  917. * cases.
  918. */
  919. void mce_notify_process(void)
  920. {
  921. unsigned long pfn;
  922. mce_notify_irq();
  923. while (mce_ring_get(&pfn))
  924. memory_failure(pfn, MCE_VECTOR);
  925. }
  926. static void mce_process_work(struct work_struct *dummy)
  927. {
  928. mce_notify_process();
  929. }
  930. #ifdef CONFIG_X86_MCE_INTEL
  931. /***
  932. * mce_log_therm_throt_event - Logs the thermal throttling event to mcelog
  933. * @cpu: The CPU on which the event occurred.
  934. * @status: Event status information
  935. *
  936. * This function should be called by the thermal interrupt after the
  937. * event has been processed and the decision was made to log the event
  938. * further.
  939. *
  940. * The status parameter will be saved to the 'status' field of 'struct mce'
  941. * and historically has been the register value of the
  942. * MSR_IA32_THERMAL_STATUS (Intel) msr.
  943. */
  944. void mce_log_therm_throt_event(__u64 status)
  945. {
  946. struct mce m;
  947. mce_setup(&m);
  948. m.bank = MCE_THERMAL_BANK;
  949. m.status = status;
  950. mce_log(&m);
  951. }
  952. #endif /* CONFIG_X86_MCE_INTEL */
  953. /*
  954. * Periodic polling timer for "silent" machine check errors. If the
  955. * poller finds an MCE, poll 2x faster. When the poller finds no more
  956. * errors, poll 2x slower (up to check_interval seconds).
  957. */
  958. static int check_interval = 5 * 60; /* 5 minutes */
  959. static DEFINE_PER_CPU(int, mce_next_interval); /* in jiffies */
  960. static DEFINE_PER_CPU(struct timer_list, mce_timer);
  961. static void mce_start_timer(unsigned long data)
  962. {
  963. struct timer_list *t = &per_cpu(mce_timer, data);
  964. int *n;
  965. WARN_ON(smp_processor_id() != data);
  966. if (mce_available(__this_cpu_ptr(&cpu_info))) {
  967. machine_check_poll(MCP_TIMESTAMP,
  968. &__get_cpu_var(mce_poll_banks));
  969. }
  970. /*
  971. * Alert userspace if needed. If we logged an MCE, reduce the
  972. * polling interval, otherwise increase the polling interval.
  973. */
  974. n = &__get_cpu_var(mce_next_interval);
  975. if (mce_notify_irq())
  976. *n = max(*n/2, HZ/100);
  977. else
  978. *n = min(*n*2, (int)round_jiffies_relative(check_interval*HZ));
  979. t->expires = jiffies + *n;
  980. add_timer_on(t, smp_processor_id());
  981. }
  982. static void mce_do_trigger(struct work_struct *work)
  983. {
  984. call_usermodehelper(mce_helper, mce_helper_argv, NULL, UMH_NO_WAIT);
  985. }
  986. static DECLARE_WORK(mce_trigger_work, mce_do_trigger);
  987. /*
  988. * Notify the user(s) about new machine check events.
  989. * Can be called from interrupt context, but not from machine check/NMI
  990. * context.
  991. */
  992. int mce_notify_irq(void)
  993. {
  994. /* Not more than two messages every minute */
  995. static DEFINE_RATELIMIT_STATE(ratelimit, 60*HZ, 2);
  996. clear_thread_flag(TIF_MCE_NOTIFY);
  997. if (test_and_clear_bit(0, &mce_need_notify)) {
  998. /* wake processes polling /dev/mcelog */
  999. wake_up_interruptible(&mce_chrdev_wait);
  1000. /*
  1001. * There is no risk of missing notifications because
  1002. * work_pending is always cleared before the function is
  1003. * executed.
  1004. */
  1005. if (mce_helper[0] && !work_pending(&mce_trigger_work))
  1006. schedule_work(&mce_trigger_work);
  1007. if (__ratelimit(&ratelimit))
  1008. pr_info(HW_ERR "Machine check events logged\n");
  1009. return 1;
  1010. }
  1011. return 0;
  1012. }
  1013. EXPORT_SYMBOL_GPL(mce_notify_irq);
  1014. static int __cpuinit __mcheck_cpu_mce_banks_init(void)
  1015. {
  1016. int i;
  1017. mce_banks = kzalloc(banks * sizeof(struct mce_bank), GFP_KERNEL);
  1018. if (!mce_banks)
  1019. return -ENOMEM;
  1020. for (i = 0; i < banks; i++) {
  1021. struct mce_bank *b = &mce_banks[i];
  1022. b->ctl = -1ULL;
  1023. b->init = 1;
  1024. }
  1025. return 0;
  1026. }
  1027. /*
  1028. * Initialize Machine Checks for a CPU.
  1029. */
  1030. static int __cpuinit __mcheck_cpu_cap_init(void)
  1031. {
  1032. unsigned b;
  1033. u64 cap;
  1034. rdmsrl(MSR_IA32_MCG_CAP, cap);
  1035. b = cap & MCG_BANKCNT_MASK;
  1036. if (!banks)
  1037. printk(KERN_INFO "mce: CPU supports %d MCE banks\n", b);
  1038. if (b > MAX_NR_BANKS) {
  1039. printk(KERN_WARNING
  1040. "MCE: Using only %u machine check banks out of %u\n",
  1041. MAX_NR_BANKS, b);
  1042. b = MAX_NR_BANKS;
  1043. }
  1044. /* Don't support asymmetric configurations today */
  1045. WARN_ON(banks != 0 && b != banks);
  1046. banks = b;
  1047. if (!mce_banks) {
  1048. int err = __mcheck_cpu_mce_banks_init();
  1049. if (err)
  1050. return err;
  1051. }
  1052. /* Use accurate RIP reporting if available. */
  1053. if ((cap & MCG_EXT_P) && MCG_EXT_CNT(cap) >= 9)
  1054. rip_msr = MSR_IA32_MCG_EIP;
  1055. if (cap & MCG_SER_P)
  1056. mce_ser = 1;
  1057. return 0;
  1058. }
  1059. static void __mcheck_cpu_init_generic(void)
  1060. {
  1061. mce_banks_t all_banks;
  1062. u64 cap;
  1063. int i;
  1064. /*
  1065. * Log the machine checks left over from the previous reset.
  1066. */
  1067. bitmap_fill(all_banks, MAX_NR_BANKS);
  1068. machine_check_poll(MCP_UC|(!mce_bootlog ? MCP_DONTLOG : 0), &all_banks);
  1069. set_in_cr4(X86_CR4_MCE);
  1070. rdmsrl(MSR_IA32_MCG_CAP, cap);
  1071. if (cap & MCG_CTL_P)
  1072. wrmsr(MSR_IA32_MCG_CTL, 0xffffffff, 0xffffffff);
  1073. for (i = 0; i < banks; i++) {
  1074. struct mce_bank *b = &mce_banks[i];
  1075. if (!b->init)
  1076. continue;
  1077. wrmsrl(MSR_IA32_MCx_CTL(i), b->ctl);
  1078. wrmsrl(MSR_IA32_MCx_STATUS(i), 0);
  1079. }
  1080. }
  1081. /* Add per CPU specific workarounds here */
  1082. static int __cpuinit __mcheck_cpu_apply_quirks(struct cpuinfo_x86 *c)
  1083. {
  1084. if (c->x86_vendor == X86_VENDOR_UNKNOWN) {
  1085. pr_info("MCE: unknown CPU type - not enabling MCE support.\n");
  1086. return -EOPNOTSUPP;
  1087. }
  1088. /* This should be disabled by the BIOS, but isn't always */
  1089. if (c->x86_vendor == X86_VENDOR_AMD) {
  1090. if (c->x86 == 15 && banks > 4) {
  1091. /*
  1092. * disable GART TBL walk error reporting, which
  1093. * trips off incorrectly with the IOMMU & 3ware
  1094. * & Cerberus:
  1095. */
  1096. clear_bit(10, (unsigned long *)&mce_banks[4].ctl);
  1097. }
  1098. if (c->x86 <= 17 && mce_bootlog < 0) {
  1099. /*
  1100. * Lots of broken BIOS around that don't clear them
  1101. * by default and leave crap in there. Don't log:
  1102. */
  1103. mce_bootlog = 0;
  1104. }
  1105. /*
  1106. * Various K7s with broken bank 0 around. Always disable
  1107. * by default.
  1108. */
  1109. if (c->x86 == 6 && banks > 0)
  1110. mce_banks[0].ctl = 0;
  1111. }
  1112. if (c->x86_vendor == X86_VENDOR_INTEL) {
  1113. /*
  1114. * SDM documents that on family 6 bank 0 should not be written
  1115. * because it aliases to another special BIOS controlled
  1116. * register.
  1117. * But it's not aliased anymore on model 0x1a+
  1118. * Don't ignore bank 0 completely because there could be a
  1119. * valid event later, merely don't write CTL0.
  1120. */
  1121. if (c->x86 == 6 && c->x86_model < 0x1A && banks > 0)
  1122. mce_banks[0].init = 0;
  1123. /*
  1124. * All newer Intel systems support MCE broadcasting. Enable
  1125. * synchronization with a one second timeout.
  1126. */
  1127. if ((c->x86 > 6 || (c->x86 == 6 && c->x86_model >= 0xe)) &&
  1128. monarch_timeout < 0)
  1129. monarch_timeout = USEC_PER_SEC;
  1130. /*
  1131. * There are also broken BIOSes on some Pentium M and
  1132. * earlier systems:
  1133. */
  1134. if (c->x86 == 6 && c->x86_model <= 13 && mce_bootlog < 0)
  1135. mce_bootlog = 0;
  1136. }
  1137. if (monarch_timeout < 0)
  1138. monarch_timeout = 0;
  1139. if (mce_bootlog != 0)
  1140. mce_panic_timeout = 30;
  1141. return 0;
  1142. }
  1143. static int __cpuinit __mcheck_cpu_ancient_init(struct cpuinfo_x86 *c)
  1144. {
  1145. if (c->x86 != 5)
  1146. return 0;
  1147. switch (c->x86_vendor) {
  1148. case X86_VENDOR_INTEL:
  1149. intel_p5_mcheck_init(c);
  1150. return 1;
  1151. break;
  1152. case X86_VENDOR_CENTAUR:
  1153. winchip_mcheck_init(c);
  1154. return 1;
  1155. break;
  1156. }
  1157. return 0;
  1158. }
  1159. static void __mcheck_cpu_init_vendor(struct cpuinfo_x86 *c)
  1160. {
  1161. switch (c->x86_vendor) {
  1162. case X86_VENDOR_INTEL:
  1163. mce_intel_feature_init(c);
  1164. break;
  1165. case X86_VENDOR_AMD:
  1166. mce_amd_feature_init(c);
  1167. break;
  1168. default:
  1169. break;
  1170. }
  1171. }
  1172. static void __mcheck_cpu_init_timer(void)
  1173. {
  1174. struct timer_list *t = &__get_cpu_var(mce_timer);
  1175. int *n = &__get_cpu_var(mce_next_interval);
  1176. setup_timer(t, mce_start_timer, smp_processor_id());
  1177. if (mce_ignore_ce)
  1178. return;
  1179. *n = check_interval * HZ;
  1180. if (!*n)
  1181. return;
  1182. t->expires = round_jiffies(jiffies + *n);
  1183. add_timer_on(t, smp_processor_id());
  1184. }
  1185. /* Handle unconfigured int18 (should never happen) */
  1186. static void unexpected_machine_check(struct pt_regs *regs, long error_code)
  1187. {
  1188. printk(KERN_ERR "CPU#%d: Unexpected int18 (Machine Check).\n",
  1189. smp_processor_id());
  1190. }
  1191. /* Call the installed machine check handler for this CPU setup. */
  1192. void (*machine_check_vector)(struct pt_regs *, long error_code) =
  1193. unexpected_machine_check;
  1194. /*
  1195. * Called for each booted CPU to set up machine checks.
  1196. * Must be called with preempt off:
  1197. */
  1198. void __cpuinit mcheck_cpu_init(struct cpuinfo_x86 *c)
  1199. {
  1200. if (mce_disabled)
  1201. return;
  1202. if (__mcheck_cpu_ancient_init(c))
  1203. return;
  1204. if (!mce_available(c))
  1205. return;
  1206. if (__mcheck_cpu_cap_init() < 0 || __mcheck_cpu_apply_quirks(c) < 0) {
  1207. mce_disabled = 1;
  1208. return;
  1209. }
  1210. machine_check_vector = do_machine_check;
  1211. __mcheck_cpu_init_generic();
  1212. __mcheck_cpu_init_vendor(c);
  1213. __mcheck_cpu_init_timer();
  1214. INIT_WORK(&__get_cpu_var(mce_work), mce_process_work);
  1215. init_irq_work(&__get_cpu_var(mce_irq_work), &mce_irq_work_cb);
  1216. }
  1217. /*
  1218. * mce_chrdev: Character device /dev/mcelog to read and clear the MCE log.
  1219. */
  1220. static DEFINE_SPINLOCK(mce_chrdev_state_lock);
  1221. static int mce_chrdev_open_count; /* #times opened */
  1222. static int mce_chrdev_open_exclu; /* already open exclusive? */
  1223. static int mce_chrdev_open(struct inode *inode, struct file *file)
  1224. {
  1225. spin_lock(&mce_chrdev_state_lock);
  1226. if (mce_chrdev_open_exclu ||
  1227. (mce_chrdev_open_count && (file->f_flags & O_EXCL))) {
  1228. spin_unlock(&mce_chrdev_state_lock);
  1229. return -EBUSY;
  1230. }
  1231. if (file->f_flags & O_EXCL)
  1232. mce_chrdev_open_exclu = 1;
  1233. mce_chrdev_open_count++;
  1234. spin_unlock(&mce_chrdev_state_lock);
  1235. return nonseekable_open(inode, file);
  1236. }
  1237. static int mce_chrdev_release(struct inode *inode, struct file *file)
  1238. {
  1239. spin_lock(&mce_chrdev_state_lock);
  1240. mce_chrdev_open_count--;
  1241. mce_chrdev_open_exclu = 0;
  1242. spin_unlock(&mce_chrdev_state_lock);
  1243. return 0;
  1244. }
  1245. static void collect_tscs(void *data)
  1246. {
  1247. unsigned long *cpu_tsc = (unsigned long *)data;
  1248. rdtscll(cpu_tsc[smp_processor_id()]);
  1249. }
  1250. static int mce_apei_read_done;
  1251. /* Collect MCE record of previous boot in persistent storage via APEI ERST. */
  1252. static int __mce_read_apei(char __user **ubuf, size_t usize)
  1253. {
  1254. int rc;
  1255. u64 record_id;
  1256. struct mce m;
  1257. if (usize < sizeof(struct mce))
  1258. return -EINVAL;
  1259. rc = apei_read_mce(&m, &record_id);
  1260. /* Error or no more MCE record */
  1261. if (rc <= 0) {
  1262. mce_apei_read_done = 1;
  1263. return rc;
  1264. }
  1265. rc = -EFAULT;
  1266. if (copy_to_user(*ubuf, &m, sizeof(struct mce)))
  1267. return rc;
  1268. /*
  1269. * In fact, we should have cleared the record after that has
  1270. * been flushed to the disk or sent to network in
  1271. * /sbin/mcelog, but we have no interface to support that now,
  1272. * so just clear it to avoid duplication.
  1273. */
  1274. rc = apei_clear_mce(record_id);
  1275. if (rc) {
  1276. mce_apei_read_done = 1;
  1277. return rc;
  1278. }
  1279. *ubuf += sizeof(struct mce);
  1280. return 0;
  1281. }
  1282. static ssize_t mce_chrdev_read(struct file *filp, char __user *ubuf,
  1283. size_t usize, loff_t *off)
  1284. {
  1285. char __user *buf = ubuf;
  1286. unsigned long *cpu_tsc;
  1287. unsigned prev, next;
  1288. int i, err;
  1289. cpu_tsc = kmalloc(nr_cpu_ids * sizeof(long), GFP_KERNEL);
  1290. if (!cpu_tsc)
  1291. return -ENOMEM;
  1292. mutex_lock(&mce_chrdev_read_mutex);
  1293. if (!mce_apei_read_done) {
  1294. err = __mce_read_apei(&buf, usize);
  1295. if (err || buf != ubuf)
  1296. goto out;
  1297. }
  1298. next = rcu_dereference_check_mce(mcelog.next);
  1299. /* Only supports full reads right now */
  1300. err = -EINVAL;
  1301. if (*off != 0 || usize < MCE_LOG_LEN*sizeof(struct mce))
  1302. goto out;
  1303. err = 0;
  1304. prev = 0;
  1305. do {
  1306. for (i = prev; i < next; i++) {
  1307. unsigned long start = jiffies;
  1308. struct mce *m = &mcelog.entry[i];
  1309. while (!m->finished) {
  1310. if (time_after_eq(jiffies, start + 2)) {
  1311. memset(m, 0, sizeof(*m));
  1312. goto timeout;
  1313. }
  1314. cpu_relax();
  1315. }
  1316. smp_rmb();
  1317. err |= copy_to_user(buf, m, sizeof(*m));
  1318. buf += sizeof(*m);
  1319. timeout:
  1320. ;
  1321. }
  1322. memset(mcelog.entry + prev, 0,
  1323. (next - prev) * sizeof(struct mce));
  1324. prev = next;
  1325. next = cmpxchg(&mcelog.next, prev, 0);
  1326. } while (next != prev);
  1327. synchronize_sched();
  1328. /*
  1329. * Collect entries that were still getting written before the
  1330. * synchronize.
  1331. */
  1332. on_each_cpu(collect_tscs, cpu_tsc, 1);
  1333. for (i = next; i < MCE_LOG_LEN; i++) {
  1334. struct mce *m = &mcelog.entry[i];
  1335. if (m->finished && m->tsc < cpu_tsc[m->cpu]) {
  1336. err |= copy_to_user(buf, m, sizeof(*m));
  1337. smp_rmb();
  1338. buf += sizeof(*m);
  1339. memset(m, 0, sizeof(*m));
  1340. }
  1341. }
  1342. if (err)
  1343. err = -EFAULT;
  1344. out:
  1345. mutex_unlock(&mce_chrdev_read_mutex);
  1346. kfree(cpu_tsc);
  1347. return err ? err : buf - ubuf;
  1348. }
  1349. static unsigned int mce_chrdev_poll(struct file *file, poll_table *wait)
  1350. {
  1351. poll_wait(file, &mce_chrdev_wait, wait);
  1352. if (rcu_access_index(mcelog.next))
  1353. return POLLIN | POLLRDNORM;
  1354. if (!mce_apei_read_done && apei_check_mce())
  1355. return POLLIN | POLLRDNORM;
  1356. return 0;
  1357. }
  1358. static long mce_chrdev_ioctl(struct file *f, unsigned int cmd,
  1359. unsigned long arg)
  1360. {
  1361. int __user *p = (int __user *)arg;
  1362. if (!capable(CAP_SYS_ADMIN))
  1363. return -EPERM;
  1364. switch (cmd) {
  1365. case MCE_GET_RECORD_LEN:
  1366. return put_user(sizeof(struct mce), p);
  1367. case MCE_GET_LOG_LEN:
  1368. return put_user(MCE_LOG_LEN, p);
  1369. case MCE_GETCLEAR_FLAGS: {
  1370. unsigned flags;
  1371. do {
  1372. flags = mcelog.flags;
  1373. } while (cmpxchg(&mcelog.flags, flags, 0) != flags);
  1374. return put_user(flags, p);
  1375. }
  1376. default:
  1377. return -ENOTTY;
  1378. }
  1379. }
  1380. /* Modified in mce-inject.c, so not static or const */
  1381. struct file_operations mce_chrdev_ops = {
  1382. .open = mce_chrdev_open,
  1383. .release = mce_chrdev_release,
  1384. .read = mce_chrdev_read,
  1385. .poll = mce_chrdev_poll,
  1386. .unlocked_ioctl = mce_chrdev_ioctl,
  1387. .llseek = no_llseek,
  1388. };
  1389. EXPORT_SYMBOL_GPL(mce_chrdev_ops);
  1390. static struct miscdevice mce_chrdev_device = {
  1391. MISC_MCELOG_MINOR,
  1392. "mcelog",
  1393. &mce_chrdev_ops,
  1394. };
  1395. /*
  1396. * mce=off Disables machine check
  1397. * mce=no_cmci Disables CMCI
  1398. * mce=dont_log_ce Clears corrected events silently, no log created for CEs.
  1399. * mce=ignore_ce Disables polling and CMCI, corrected events are not cleared.
  1400. * mce=TOLERANCELEVEL[,monarchtimeout] (number, see above)
  1401. * monarchtimeout is how long to wait for other CPUs on machine
  1402. * check, or 0 to not wait
  1403. * mce=bootlog Log MCEs from before booting. Disabled by default on AMD.
  1404. * mce=nobootlog Don't log MCEs from before booting.
  1405. */
  1406. static int __init mcheck_enable(char *str)
  1407. {
  1408. if (*str == 0) {
  1409. enable_p5_mce();
  1410. return 1;
  1411. }
  1412. if (*str == '=')
  1413. str++;
  1414. if (!strcmp(str, "off"))
  1415. mce_disabled = 1;
  1416. else if (!strcmp(str, "no_cmci"))
  1417. mce_cmci_disabled = 1;
  1418. else if (!strcmp(str, "dont_log_ce"))
  1419. mce_dont_log_ce = 1;
  1420. else if (!strcmp(str, "ignore_ce"))
  1421. mce_ignore_ce = 1;
  1422. else if (!strcmp(str, "bootlog") || !strcmp(str, "nobootlog"))
  1423. mce_bootlog = (str[0] == 'b');
  1424. else if (isdigit(str[0])) {
  1425. get_option(&str, &tolerant);
  1426. if (*str == ',') {
  1427. ++str;
  1428. get_option(&str, &monarch_timeout);
  1429. }
  1430. } else {
  1431. printk(KERN_INFO "mce argument %s ignored. Please use /sys\n",
  1432. str);
  1433. return 0;
  1434. }
  1435. return 1;
  1436. }
  1437. __setup("mce", mcheck_enable);
  1438. int __init mcheck_init(void)
  1439. {
  1440. mcheck_intel_therm_init();
  1441. return 0;
  1442. }
  1443. /*
  1444. * mce_syscore: PM support
  1445. */
  1446. /*
  1447. * Disable machine checks on suspend and shutdown. We can't really handle
  1448. * them later.
  1449. */
  1450. static int mce_disable_error_reporting(void)
  1451. {
  1452. int i;
  1453. for (i = 0; i < banks; i++) {
  1454. struct mce_bank *b = &mce_banks[i];
  1455. if (b->init)
  1456. wrmsrl(MSR_IA32_MCx_CTL(i), 0);
  1457. }
  1458. return 0;
  1459. }
  1460. static int mce_syscore_suspend(void)
  1461. {
  1462. return mce_disable_error_reporting();
  1463. }
  1464. static void mce_syscore_shutdown(void)
  1465. {
  1466. mce_disable_error_reporting();
  1467. }
  1468. /*
  1469. * On resume clear all MCE state. Don't want to see leftovers from the BIOS.
  1470. * Only one CPU is active at this time, the others get re-added later using
  1471. * CPU hotplug:
  1472. */
  1473. static void mce_syscore_resume(void)
  1474. {
  1475. __mcheck_cpu_init_generic();
  1476. __mcheck_cpu_init_vendor(__this_cpu_ptr(&cpu_info));
  1477. }
  1478. static struct syscore_ops mce_syscore_ops = {
  1479. .suspend = mce_syscore_suspend,
  1480. .shutdown = mce_syscore_shutdown,
  1481. .resume = mce_syscore_resume,
  1482. };
  1483. /*
  1484. * mce_sysdev: Sysfs support
  1485. */
  1486. static void mce_cpu_restart(void *data)
  1487. {
  1488. del_timer_sync(&__get_cpu_var(mce_timer));
  1489. if (!mce_available(__this_cpu_ptr(&cpu_info)))
  1490. return;
  1491. __mcheck_cpu_init_generic();
  1492. __mcheck_cpu_init_timer();
  1493. }
  1494. /* Reinit MCEs after user configuration changes */
  1495. static void mce_restart(void)
  1496. {
  1497. on_each_cpu(mce_cpu_restart, NULL, 1);
  1498. }
  1499. /* Toggle features for corrected errors */
  1500. static void mce_disable_ce(void *all)
  1501. {
  1502. if (!mce_available(__this_cpu_ptr(&cpu_info)))
  1503. return;
  1504. if (all)
  1505. del_timer_sync(&__get_cpu_var(mce_timer));
  1506. cmci_clear();
  1507. }
  1508. static void mce_enable_ce(void *all)
  1509. {
  1510. if (!mce_available(__this_cpu_ptr(&cpu_info)))
  1511. return;
  1512. cmci_reenable();
  1513. cmci_recheck();
  1514. if (all)
  1515. __mcheck_cpu_init_timer();
  1516. }
  1517. static struct sysdev_class mce_sysdev_class = {
  1518. .name = "machinecheck",
  1519. };
  1520. DEFINE_PER_CPU(struct sys_device, mce_sysdev);
  1521. __cpuinitdata
  1522. void (*threshold_cpu_callback)(unsigned long action, unsigned int cpu);
  1523. static inline struct mce_bank *attr_to_bank(struct sysdev_attribute *attr)
  1524. {
  1525. return container_of(attr, struct mce_bank, attr);
  1526. }
  1527. static ssize_t show_bank(struct sys_device *s, struct sysdev_attribute *attr,
  1528. char *buf)
  1529. {
  1530. return sprintf(buf, "%llx\n", attr_to_bank(attr)->ctl);
  1531. }
  1532. static ssize_t set_bank(struct sys_device *s, struct sysdev_attribute *attr,
  1533. const char *buf, size_t size)
  1534. {
  1535. u64 new;
  1536. if (strict_strtoull(buf, 0, &new) < 0)
  1537. return -EINVAL;
  1538. attr_to_bank(attr)->ctl = new;
  1539. mce_restart();
  1540. return size;
  1541. }
  1542. static ssize_t
  1543. show_trigger(struct sys_device *s, struct sysdev_attribute *attr, char *buf)
  1544. {
  1545. strcpy(buf, mce_helper);
  1546. strcat(buf, "\n");
  1547. return strlen(mce_helper) + 1;
  1548. }
  1549. static ssize_t set_trigger(struct sys_device *s, struct sysdev_attribute *attr,
  1550. const char *buf, size_t siz)
  1551. {
  1552. char *p;
  1553. strncpy(mce_helper, buf, sizeof(mce_helper));
  1554. mce_helper[sizeof(mce_helper)-1] = 0;
  1555. p = strchr(mce_helper, '\n');
  1556. if (p)
  1557. *p = 0;
  1558. return strlen(mce_helper) + !!p;
  1559. }
  1560. static ssize_t set_ignore_ce(struct sys_device *s,
  1561. struct sysdev_attribute *attr,
  1562. const char *buf, size_t size)
  1563. {
  1564. u64 new;
  1565. if (strict_strtoull(buf, 0, &new) < 0)
  1566. return -EINVAL;
  1567. if (mce_ignore_ce ^ !!new) {
  1568. if (new) {
  1569. /* disable ce features */
  1570. on_each_cpu(mce_disable_ce, (void *)1, 1);
  1571. mce_ignore_ce = 1;
  1572. } else {
  1573. /* enable ce features */
  1574. mce_ignore_ce = 0;
  1575. on_each_cpu(mce_enable_ce, (void *)1, 1);
  1576. }
  1577. }
  1578. return size;
  1579. }
  1580. static ssize_t set_cmci_disabled(struct sys_device *s,
  1581. struct sysdev_attribute *attr,
  1582. const char *buf, size_t size)
  1583. {
  1584. u64 new;
  1585. if (strict_strtoull(buf, 0, &new) < 0)
  1586. return -EINVAL;
  1587. if (mce_cmci_disabled ^ !!new) {
  1588. if (new) {
  1589. /* disable cmci */
  1590. on_each_cpu(mce_disable_ce, NULL, 1);
  1591. mce_cmci_disabled = 1;
  1592. } else {
  1593. /* enable cmci */
  1594. mce_cmci_disabled = 0;
  1595. on_each_cpu(mce_enable_ce, NULL, 1);
  1596. }
  1597. }
  1598. return size;
  1599. }
  1600. static ssize_t store_int_with_restart(struct sys_device *s,
  1601. struct sysdev_attribute *attr,
  1602. const char *buf, size_t size)
  1603. {
  1604. ssize_t ret = sysdev_store_int(s, attr, buf, size);
  1605. mce_restart();
  1606. return ret;
  1607. }
  1608. static SYSDEV_ATTR(trigger, 0644, show_trigger, set_trigger);
  1609. static SYSDEV_INT_ATTR(tolerant, 0644, tolerant);
  1610. static SYSDEV_INT_ATTR(monarch_timeout, 0644, monarch_timeout);
  1611. static SYSDEV_INT_ATTR(dont_log_ce, 0644, mce_dont_log_ce);
  1612. static struct sysdev_ext_attribute attr_check_interval = {
  1613. _SYSDEV_ATTR(check_interval, 0644, sysdev_show_int,
  1614. store_int_with_restart),
  1615. &check_interval
  1616. };
  1617. static struct sysdev_ext_attribute attr_ignore_ce = {
  1618. _SYSDEV_ATTR(ignore_ce, 0644, sysdev_show_int, set_ignore_ce),
  1619. &mce_ignore_ce
  1620. };
  1621. static struct sysdev_ext_attribute attr_cmci_disabled = {
  1622. _SYSDEV_ATTR(cmci_disabled, 0644, sysdev_show_int, set_cmci_disabled),
  1623. &mce_cmci_disabled
  1624. };
  1625. static struct sysdev_attribute *mce_sysdev_attrs[] = {
  1626. &attr_tolerant.attr,
  1627. &attr_check_interval.attr,
  1628. &attr_trigger,
  1629. &attr_monarch_timeout.attr,
  1630. &attr_dont_log_ce.attr,
  1631. &attr_ignore_ce.attr,
  1632. &attr_cmci_disabled.attr,
  1633. NULL
  1634. };
  1635. static cpumask_var_t mce_sysdev_initialized;
  1636. /* Per cpu sysdev init. All of the cpus still share the same ctrl bank: */
  1637. static __cpuinit int mce_sysdev_create(unsigned int cpu)
  1638. {
  1639. struct sys_device *sysdev = &per_cpu(mce_sysdev, cpu);
  1640. int err;
  1641. int i, j;
  1642. if (!mce_available(&boot_cpu_data))
  1643. return -EIO;
  1644. memset(&sysdev->kobj, 0, sizeof(struct kobject));
  1645. sysdev->id = cpu;
  1646. sysdev->cls = &mce_sysdev_class;
  1647. err = sysdev_register(sysdev);
  1648. if (err)
  1649. return err;
  1650. for (i = 0; mce_sysdev_attrs[i]; i++) {
  1651. err = sysdev_create_file(sysdev, mce_sysdev_attrs[i]);
  1652. if (err)
  1653. goto error;
  1654. }
  1655. for (j = 0; j < banks; j++) {
  1656. err = sysdev_create_file(sysdev, &mce_banks[j].attr);
  1657. if (err)
  1658. goto error2;
  1659. }
  1660. cpumask_set_cpu(cpu, mce_sysdev_initialized);
  1661. return 0;
  1662. error2:
  1663. while (--j >= 0)
  1664. sysdev_remove_file(sysdev, &mce_banks[j].attr);
  1665. error:
  1666. while (--i >= 0)
  1667. sysdev_remove_file(sysdev, mce_sysdev_attrs[i]);
  1668. sysdev_unregister(sysdev);
  1669. return err;
  1670. }
  1671. static __cpuinit void mce_sysdev_remove(unsigned int cpu)
  1672. {
  1673. struct sys_device *sysdev = &per_cpu(mce_sysdev, cpu);
  1674. int i;
  1675. if (!cpumask_test_cpu(cpu, mce_sysdev_initialized))
  1676. return;
  1677. for (i = 0; mce_sysdev_attrs[i]; i++)
  1678. sysdev_remove_file(sysdev, mce_sysdev_attrs[i]);
  1679. for (i = 0; i < banks; i++)
  1680. sysdev_remove_file(sysdev, &mce_banks[i].attr);
  1681. sysdev_unregister(sysdev);
  1682. cpumask_clear_cpu(cpu, mce_sysdev_initialized);
  1683. }
  1684. /* Make sure there are no machine checks on offlined CPUs. */
  1685. static void __cpuinit mce_disable_cpu(void *h)
  1686. {
  1687. unsigned long action = *(unsigned long *)h;
  1688. int i;
  1689. if (!mce_available(__this_cpu_ptr(&cpu_info)))
  1690. return;
  1691. if (!(action & CPU_TASKS_FROZEN))
  1692. cmci_clear();
  1693. for (i = 0; i < banks; i++) {
  1694. struct mce_bank *b = &mce_banks[i];
  1695. if (b->init)
  1696. wrmsrl(MSR_IA32_MCx_CTL(i), 0);
  1697. }
  1698. }
  1699. static void __cpuinit mce_reenable_cpu(void *h)
  1700. {
  1701. unsigned long action = *(unsigned long *)h;
  1702. int i;
  1703. if (!mce_available(__this_cpu_ptr(&cpu_info)))
  1704. return;
  1705. if (!(action & CPU_TASKS_FROZEN))
  1706. cmci_reenable();
  1707. for (i = 0; i < banks; i++) {
  1708. struct mce_bank *b = &mce_banks[i];
  1709. if (b->init)
  1710. wrmsrl(MSR_IA32_MCx_CTL(i), b->ctl);
  1711. }
  1712. }
  1713. /* Get notified when a cpu comes on/off. Be hotplug friendly. */
  1714. static int __cpuinit
  1715. mce_cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
  1716. {
  1717. unsigned int cpu = (unsigned long)hcpu;
  1718. struct timer_list *t = &per_cpu(mce_timer, cpu);
  1719. switch (action) {
  1720. case CPU_ONLINE:
  1721. case CPU_ONLINE_FROZEN:
  1722. mce_sysdev_create(cpu);
  1723. if (threshold_cpu_callback)
  1724. threshold_cpu_callback(action, cpu);
  1725. break;
  1726. case CPU_DEAD:
  1727. case CPU_DEAD_FROZEN:
  1728. if (threshold_cpu_callback)
  1729. threshold_cpu_callback(action, cpu);
  1730. mce_sysdev_remove(cpu);
  1731. break;
  1732. case CPU_DOWN_PREPARE:
  1733. case CPU_DOWN_PREPARE_FROZEN:
  1734. del_timer_sync(t);
  1735. smp_call_function_single(cpu, mce_disable_cpu, &action, 1);
  1736. break;
  1737. case CPU_DOWN_FAILED:
  1738. case CPU_DOWN_FAILED_FROZEN:
  1739. if (!mce_ignore_ce && check_interval) {
  1740. t->expires = round_jiffies(jiffies +
  1741. __get_cpu_var(mce_next_interval));
  1742. add_timer_on(t, cpu);
  1743. }
  1744. smp_call_function_single(cpu, mce_reenable_cpu, &action, 1);
  1745. break;
  1746. case CPU_POST_DEAD:
  1747. /* intentionally ignoring frozen here */
  1748. cmci_rediscover(cpu);
  1749. break;
  1750. }
  1751. return NOTIFY_OK;
  1752. }
  1753. static struct notifier_block mce_cpu_notifier __cpuinitdata = {
  1754. .notifier_call = mce_cpu_callback,
  1755. };
  1756. static __init void mce_init_banks(void)
  1757. {
  1758. int i;
  1759. for (i = 0; i < banks; i++) {
  1760. struct mce_bank *b = &mce_banks[i];
  1761. struct sysdev_attribute *a = &b->attr;
  1762. sysfs_attr_init(&a->attr);
  1763. a->attr.name = b->attrname;
  1764. snprintf(b->attrname, ATTR_LEN, "bank%d", i);
  1765. a->attr.mode = 0644;
  1766. a->show = show_bank;
  1767. a->store = set_bank;
  1768. }
  1769. }
  1770. static __init int mcheck_init_device(void)
  1771. {
  1772. int err;
  1773. int i = 0;
  1774. if (!mce_available(&boot_cpu_data))
  1775. return -EIO;
  1776. zalloc_cpumask_var(&mce_sysdev_initialized, GFP_KERNEL);
  1777. mce_init_banks();
  1778. err = sysdev_class_register(&mce_sysdev_class);
  1779. if (err)
  1780. return err;
  1781. for_each_online_cpu(i) {
  1782. err = mce_sysdev_create(i);
  1783. if (err)
  1784. return err;
  1785. }
  1786. register_syscore_ops(&mce_syscore_ops);
  1787. register_hotcpu_notifier(&mce_cpu_notifier);
  1788. /* register character device /dev/mcelog */
  1789. misc_register(&mce_chrdev_device);
  1790. return err;
  1791. }
  1792. device_initcall(mcheck_init_device);
  1793. /*
  1794. * Old style boot options parsing. Only for compatibility.
  1795. */
  1796. static int __init mcheck_disable(char *str)
  1797. {
  1798. mce_disabled = 1;
  1799. return 1;
  1800. }
  1801. __setup("nomce", mcheck_disable);
  1802. #ifdef CONFIG_DEBUG_FS
  1803. struct dentry *mce_get_debugfs_dir(void)
  1804. {
  1805. static struct dentry *dmce;
  1806. if (!dmce)
  1807. dmce = debugfs_create_dir("mce", NULL);
  1808. return dmce;
  1809. }
  1810. static void mce_reset(void)
  1811. {
  1812. cpu_missing = 0;
  1813. atomic_set(&mce_fake_paniced, 0);
  1814. atomic_set(&mce_executing, 0);
  1815. atomic_set(&mce_callin, 0);
  1816. atomic_set(&global_nwo, 0);
  1817. }
  1818. static int fake_panic_get(void *data, u64 *val)
  1819. {
  1820. *val = fake_panic;
  1821. return 0;
  1822. }
  1823. static int fake_panic_set(void *data, u64 val)
  1824. {
  1825. mce_reset();
  1826. fake_panic = val;
  1827. return 0;
  1828. }
  1829. DEFINE_SIMPLE_ATTRIBUTE(fake_panic_fops, fake_panic_get,
  1830. fake_panic_set, "%llu\n");
  1831. static int __init mcheck_debugfs_init(void)
  1832. {
  1833. struct dentry *dmce, *ffake_panic;
  1834. dmce = mce_get_debugfs_dir();
  1835. if (!dmce)
  1836. return -ENOMEM;
  1837. ffake_panic = debugfs_create_file("fake_panic", 0444, dmce, NULL,
  1838. &fake_panic_fops);
  1839. if (!ffake_panic)
  1840. return -ENOMEM;
  1841. return 0;
  1842. }
  1843. late_initcall(mcheck_debugfs_init);
  1844. #endif