mce.c 45 KB

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