mce.c 50 KB

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