mce.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964
  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;
  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 = 1;
  69. static int banks;
  70. static u64 *bank;
  71. static unsigned long notify_user;
  72. static int rip_msr;
  73. static int mce_bootlog = -1;
  74. static int monarch_timeout = -1;
  75. static int mce_panic_timeout;
  76. static int mce_dont_log_ce;
  77. int mce_cmci_disabled;
  78. int mce_ignore_ce;
  79. int mce_ser;
  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, int *order)
  614. {
  615. int nwo;
  616. int cpus = num_online_cpus();
  617. u64 timeout = (u64)monarch_timeout * NSEC_PER_USEC;
  618. if (!timeout) {
  619. *order = -1;
  620. return no_way_out;
  621. }
  622. atomic_add(no_way_out, &global_nwo);
  623. /*
  624. * Wait for everyone.
  625. */
  626. while (atomic_read(&mce_callin) != cpus) {
  627. if (mce_timed_out(&timeout)) {
  628. atomic_set(&global_nwo, 0);
  629. *order = -1;
  630. return no_way_out;
  631. }
  632. ndelay(SPINUNIT);
  633. }
  634. /*
  635. * Cache the global no_way_out state.
  636. */
  637. nwo = atomic_read(&global_nwo);
  638. /*
  639. * Monarch starts executing now, the others wait.
  640. */
  641. if (*order == 1) {
  642. atomic_set(&mce_executing, 1);
  643. return nwo;
  644. }
  645. /*
  646. * Now start the scanning loop one by one
  647. * in the original callin order.
  648. * This way when there are any shared banks it will
  649. * be only seen by one CPU before cleared, avoiding duplicates.
  650. */
  651. while (atomic_read(&mce_executing) < *order) {
  652. if (mce_timed_out(&timeout)) {
  653. atomic_set(&global_nwo, 0);
  654. *order = -1;
  655. return no_way_out;
  656. }
  657. ndelay(SPINUNIT);
  658. }
  659. return nwo;
  660. }
  661. /*
  662. * Synchronize between CPUs after main scanning loop.
  663. * This invokes the bulk of the Monarch processing.
  664. */
  665. static int mce_end(int order)
  666. {
  667. int ret = -1;
  668. u64 timeout = (u64)monarch_timeout * NSEC_PER_USEC;
  669. if (!timeout)
  670. goto reset;
  671. if (order < 0)
  672. goto reset;
  673. /*
  674. * Allow others to run.
  675. */
  676. atomic_inc(&mce_executing);
  677. if (order == 1) {
  678. /* CHECKME: Can this race with a parallel hotplug? */
  679. int cpus = num_online_cpus();
  680. /*
  681. * Monarch: Wait for everyone to go through their scanning
  682. * loops.
  683. */
  684. while (atomic_read(&mce_executing) <= cpus) {
  685. if (mce_timed_out(&timeout))
  686. goto reset;
  687. ndelay(SPINUNIT);
  688. }
  689. mce_reign();
  690. barrier();
  691. ret = 0;
  692. } else {
  693. /*
  694. * Subject: Wait for Monarch to finish.
  695. */
  696. while (atomic_read(&mce_executing) != 0) {
  697. if (mce_timed_out(&timeout))
  698. goto reset;
  699. ndelay(SPINUNIT);
  700. }
  701. /*
  702. * Don't reset anything. That's done by the Monarch.
  703. */
  704. return 0;
  705. }
  706. /*
  707. * Reset all global state.
  708. */
  709. reset:
  710. atomic_set(&global_nwo, 0);
  711. atomic_set(&mce_callin, 0);
  712. barrier();
  713. /*
  714. * Let others run again.
  715. */
  716. atomic_set(&mce_executing, 0);
  717. return ret;
  718. }
  719. /*
  720. * Check if the address reported by the CPU is in a format we can parse.
  721. * It would be possible to add code for most other cases, but all would
  722. * be somewhat complicated (e.g. segment offset would require an instruction
  723. * parser). So only support physical addresses upto page granuality for now.
  724. */
  725. static int mce_usable_address(struct mce *m)
  726. {
  727. if (!(m->status & MCI_STATUS_MISCV) || !(m->status & MCI_STATUS_ADDRV))
  728. return 0;
  729. if ((m->misc & 0x3f) > PAGE_SHIFT)
  730. return 0;
  731. if (((m->misc >> 6) & 7) != MCM_ADDR_PHYS)
  732. return 0;
  733. return 1;
  734. }
  735. static void mce_clear_state(unsigned long *toclear)
  736. {
  737. int i;
  738. for (i = 0; i < banks; i++) {
  739. if (test_bit(i, toclear))
  740. mce_wrmsrl(MSR_IA32_MC0_STATUS+4*i, 0);
  741. }
  742. }
  743. /*
  744. * The actual machine check handler. This only handles real
  745. * exceptions when something got corrupted coming in through int 18.
  746. *
  747. * This is executed in NMI context not subject to normal locking rules. This
  748. * implies that most kernel services cannot be safely used. Don't even
  749. * think about putting a printk in there!
  750. *
  751. * On Intel systems this is entered on all CPUs in parallel through
  752. * MCE broadcast. However some CPUs might be broken beyond repair,
  753. * so be always careful when synchronizing with others.
  754. */
  755. void do_machine_check(struct pt_regs *regs, long error_code)
  756. {
  757. struct mce m, *final;
  758. int i;
  759. int worst = 0;
  760. int severity;
  761. /*
  762. * Establish sequential order between the CPUs entering the machine
  763. * check handler.
  764. */
  765. int order;
  766. /*
  767. * If no_way_out gets set, there is no safe way to recover from this
  768. * MCE. If tolerant is cranked up, we'll try anyway.
  769. */
  770. int no_way_out = 0;
  771. /*
  772. * If kill_it gets set, there might be a way to recover from this
  773. * error.
  774. */
  775. int kill_it = 0;
  776. DECLARE_BITMAP(toclear, MAX_NR_BANKS);
  777. char *msg = "Unknown";
  778. atomic_inc(&mce_entry);
  779. __get_cpu_var(mce_exception_count)++;
  780. if (notify_die(DIE_NMI, "machine check", regs, error_code,
  781. 18, SIGKILL) == NOTIFY_STOP)
  782. goto out;
  783. if (!banks)
  784. goto out;
  785. order = atomic_add_return(1, &mce_callin);
  786. mce_setup(&m);
  787. m.mcgstatus = mce_rdmsrl(MSR_IA32_MCG_STATUS);
  788. no_way_out = mce_no_way_out(&m, &msg);
  789. final = &__get_cpu_var(mces_seen);
  790. *final = m;
  791. barrier();
  792. /*
  793. * When no restart IP must always kill or panic.
  794. */
  795. if (!(m.mcgstatus & MCG_STATUS_RIPV))
  796. kill_it = 1;
  797. /*
  798. * Go through all the banks in exclusion of the other CPUs.
  799. * This way we don't report duplicated events on shared banks
  800. * because the first one to see it will clear it.
  801. */
  802. no_way_out = mce_start(no_way_out, &order);
  803. for (i = 0; i < banks; i++) {
  804. __clear_bit(i, toclear);
  805. if (!bank[i])
  806. continue;
  807. m.misc = 0;
  808. m.addr = 0;
  809. m.bank = i;
  810. m.status = mce_rdmsrl(MSR_IA32_MC0_STATUS + i*4);
  811. if ((m.status & MCI_STATUS_VAL) == 0)
  812. continue;
  813. /*
  814. * Non uncorrected or non signaled errors are handled by
  815. * machine_check_poll. Leave them alone, unless this panics.
  816. */
  817. if (!(m.status & (mce_ser ? MCI_STATUS_S : MCI_STATUS_UC)) &&
  818. !no_way_out)
  819. continue;
  820. /*
  821. * Set taint even when machine check was not enabled.
  822. */
  823. add_taint(TAINT_MACHINE_CHECK);
  824. severity = mce_severity(&m, tolerant, NULL);
  825. /*
  826. * When machine check was for corrected handler don't touch,
  827. * unless we're panicing.
  828. */
  829. if (severity == MCE_KEEP_SEVERITY && !no_way_out)
  830. continue;
  831. __set_bit(i, toclear);
  832. if (severity == MCE_NO_SEVERITY) {
  833. /*
  834. * Machine check event was not enabled. Clear, but
  835. * ignore.
  836. */
  837. continue;
  838. }
  839. /*
  840. * Kill on action required.
  841. */
  842. if (severity == MCE_AR_SEVERITY)
  843. kill_it = 1;
  844. if (m.status & MCI_STATUS_MISCV)
  845. m.misc = mce_rdmsrl(MSR_IA32_MC0_MISC + i*4);
  846. if (m.status & MCI_STATUS_ADDRV)
  847. m.addr = mce_rdmsrl(MSR_IA32_MC0_ADDR + i*4);
  848. /*
  849. * Action optional error. Queue address for later processing.
  850. * When the ring overflows we just ignore the AO error.
  851. * RED-PEN add some logging mechanism when
  852. * usable_address or mce_add_ring fails.
  853. * RED-PEN don't ignore overflow for tolerant == 0
  854. */
  855. if (severity == MCE_AO_SEVERITY && mce_usable_address(&m))
  856. mce_ring_add(m.addr >> PAGE_SHIFT);
  857. mce_get_rip(&m, regs);
  858. mce_log(&m);
  859. if (severity > worst) {
  860. *final = m;
  861. worst = severity;
  862. }
  863. }
  864. if (!no_way_out)
  865. mce_clear_state(toclear);
  866. /*
  867. * Do most of the synchronization with other CPUs.
  868. * When there's any problem use only local no_way_out state.
  869. */
  870. if (mce_end(order) < 0)
  871. no_way_out = worst >= MCE_PANIC_SEVERITY;
  872. /*
  873. * If we have decided that we just CAN'T continue, and the user
  874. * has not set tolerant to an insane level, give up and die.
  875. *
  876. * This is mainly used in the case when the system doesn't
  877. * support MCE broadcasting or it has been disabled.
  878. */
  879. if (no_way_out && tolerant < 3)
  880. mce_panic("Fatal machine check on current CPU", final, msg);
  881. /*
  882. * If the error seems to be unrecoverable, something should be
  883. * done. Try to kill as little as possible. If we can kill just
  884. * one task, do that. If the user has set the tolerance very
  885. * high, don't try to do anything at all.
  886. */
  887. if (kill_it && tolerant < 3)
  888. force_sig(SIGBUS, current);
  889. /* notify userspace ASAP */
  890. set_thread_flag(TIF_MCE_NOTIFY);
  891. if (worst > 0)
  892. mce_report_event(regs);
  893. mce_wrmsrl(MSR_IA32_MCG_STATUS, 0);
  894. out:
  895. atomic_dec(&mce_entry);
  896. sync_core();
  897. }
  898. EXPORT_SYMBOL_GPL(do_machine_check);
  899. /* dummy to break dependency. actual code is in mm/memory-failure.c */
  900. void __attribute__((weak)) memory_failure(unsigned long pfn, int vector)
  901. {
  902. printk(KERN_ERR "Action optional memory failure at %lx ignored\n", pfn);
  903. }
  904. /*
  905. * Called after mce notification in process context. This code
  906. * is allowed to sleep. Call the high level VM handler to process
  907. * any corrupted pages.
  908. * Assume that the work queue code only calls this one at a time
  909. * per CPU.
  910. * Note we don't disable preemption, so this code might run on the wrong
  911. * CPU. In this case the event is picked up by the scheduled work queue.
  912. * This is merely a fast path to expedite processing in some common
  913. * cases.
  914. */
  915. void mce_notify_process(void)
  916. {
  917. unsigned long pfn;
  918. mce_notify_irq();
  919. while (mce_ring_get(&pfn))
  920. memory_failure(pfn, MCE_VECTOR);
  921. }
  922. static void mce_process_work(struct work_struct *dummy)
  923. {
  924. mce_notify_process();
  925. }
  926. #ifdef CONFIG_X86_MCE_INTEL
  927. /***
  928. * mce_log_therm_throt_event - Logs the thermal throttling event to mcelog
  929. * @cpu: The CPU on which the event occurred.
  930. * @status: Event status information
  931. *
  932. * This function should be called by the thermal interrupt after the
  933. * event has been processed and the decision was made to log the event
  934. * further.
  935. *
  936. * The status parameter will be saved to the 'status' field of 'struct mce'
  937. * and historically has been the register value of the
  938. * MSR_IA32_THERMAL_STATUS (Intel) msr.
  939. */
  940. void mce_log_therm_throt_event(__u64 status)
  941. {
  942. struct mce m;
  943. mce_setup(&m);
  944. m.bank = MCE_THERMAL_BANK;
  945. m.status = status;
  946. mce_log(&m);
  947. }
  948. #endif /* CONFIG_X86_MCE_INTEL */
  949. /*
  950. * Periodic polling timer for "silent" machine check errors. If the
  951. * poller finds an MCE, poll 2x faster. When the poller finds no more
  952. * errors, poll 2x slower (up to check_interval seconds).
  953. */
  954. static int check_interval = 5 * 60; /* 5 minutes */
  955. static DEFINE_PER_CPU(int, next_interval); /* in jiffies */
  956. static DEFINE_PER_CPU(struct timer_list, mce_timer);
  957. static void mcheck_timer(unsigned long data)
  958. {
  959. struct timer_list *t = &per_cpu(mce_timer, data);
  960. int *n;
  961. WARN_ON(smp_processor_id() != data);
  962. if (mce_available(&current_cpu_data)) {
  963. machine_check_poll(MCP_TIMESTAMP,
  964. &__get_cpu_var(mce_poll_banks));
  965. }
  966. /*
  967. * Alert userspace if needed. If we logged an MCE, reduce the
  968. * polling interval, otherwise increase the polling interval.
  969. */
  970. n = &__get_cpu_var(next_interval);
  971. if (mce_notify_irq())
  972. *n = max(*n/2, HZ/100);
  973. else
  974. *n = min(*n*2, (int)round_jiffies_relative(check_interval*HZ));
  975. t->expires = jiffies + *n;
  976. add_timer(t);
  977. }
  978. static void mce_do_trigger(struct work_struct *work)
  979. {
  980. call_usermodehelper(trigger, trigger_argv, NULL, UMH_NO_WAIT);
  981. }
  982. static DECLARE_WORK(mce_trigger_work, mce_do_trigger);
  983. /*
  984. * Notify the user(s) about new machine check events.
  985. * Can be called from interrupt context, but not from machine check/NMI
  986. * context.
  987. */
  988. int mce_notify_irq(void)
  989. {
  990. /* Not more than two messages every minute */
  991. static DEFINE_RATELIMIT_STATE(ratelimit, 60*HZ, 2);
  992. clear_thread_flag(TIF_MCE_NOTIFY);
  993. if (test_and_clear_bit(0, &notify_user)) {
  994. wake_up_interruptible(&mce_wait);
  995. /*
  996. * There is no risk of missing notifications because
  997. * work_pending is always cleared before the function is
  998. * executed.
  999. */
  1000. if (trigger[0] && !work_pending(&mce_trigger_work))
  1001. schedule_work(&mce_trigger_work);
  1002. if (__ratelimit(&ratelimit))
  1003. printk(KERN_INFO "Machine check events logged\n");
  1004. return 1;
  1005. }
  1006. return 0;
  1007. }
  1008. EXPORT_SYMBOL_GPL(mce_notify_irq);
  1009. /*
  1010. * Initialize Machine Checks for a CPU.
  1011. */
  1012. static int mce_cap_init(void)
  1013. {
  1014. unsigned b;
  1015. u64 cap;
  1016. rdmsrl(MSR_IA32_MCG_CAP, cap);
  1017. b = cap & MCG_BANKCNT_MASK;
  1018. printk(KERN_INFO "mce: CPU supports %d MCE banks\n", b);
  1019. if (b > MAX_NR_BANKS) {
  1020. printk(KERN_WARNING
  1021. "MCE: Using only %u machine check banks out of %u\n",
  1022. MAX_NR_BANKS, b);
  1023. b = MAX_NR_BANKS;
  1024. }
  1025. /* Don't support asymmetric configurations today */
  1026. WARN_ON(banks != 0 && b != banks);
  1027. banks = b;
  1028. if (!bank) {
  1029. bank = kmalloc(banks * sizeof(u64), GFP_KERNEL);
  1030. if (!bank)
  1031. return -ENOMEM;
  1032. memset(bank, 0xff, banks * sizeof(u64));
  1033. }
  1034. /* Use accurate RIP reporting if available. */
  1035. if ((cap & MCG_EXT_P) && MCG_EXT_CNT(cap) >= 9)
  1036. rip_msr = MSR_IA32_MCG_EIP;
  1037. if (cap & MCG_SER_P)
  1038. mce_ser = 1;
  1039. return 0;
  1040. }
  1041. static void mce_init(void)
  1042. {
  1043. mce_banks_t all_banks;
  1044. u64 cap;
  1045. int i;
  1046. /*
  1047. * Log the machine checks left over from the previous reset.
  1048. */
  1049. bitmap_fill(all_banks, MAX_NR_BANKS);
  1050. machine_check_poll(MCP_UC|(!mce_bootlog ? MCP_DONTLOG : 0), &all_banks);
  1051. set_in_cr4(X86_CR4_MCE);
  1052. rdmsrl(MSR_IA32_MCG_CAP, cap);
  1053. if (cap & MCG_CTL_P)
  1054. wrmsr(MSR_IA32_MCG_CTL, 0xffffffff, 0xffffffff);
  1055. for (i = 0; i < banks; i++) {
  1056. if (skip_bank_init(i))
  1057. continue;
  1058. wrmsrl(MSR_IA32_MC0_CTL+4*i, bank[i]);
  1059. wrmsrl(MSR_IA32_MC0_STATUS+4*i, 0);
  1060. }
  1061. }
  1062. /* Add per CPU specific workarounds here */
  1063. static void mce_cpu_quirks(struct cpuinfo_x86 *c)
  1064. {
  1065. /* This should be disabled by the BIOS, but isn't always */
  1066. if (c->x86_vendor == X86_VENDOR_AMD) {
  1067. if (c->x86 == 15 && banks > 4) {
  1068. /*
  1069. * disable GART TBL walk error reporting, which
  1070. * trips off incorrectly with the IOMMU & 3ware
  1071. * & Cerberus:
  1072. */
  1073. clear_bit(10, (unsigned long *)&bank[4]);
  1074. }
  1075. if (c->x86 <= 17 && mce_bootlog < 0) {
  1076. /*
  1077. * Lots of broken BIOS around that don't clear them
  1078. * by default and leave crap in there. Don't log:
  1079. */
  1080. mce_bootlog = 0;
  1081. }
  1082. /*
  1083. * Various K7s with broken bank 0 around. Always disable
  1084. * by default.
  1085. */
  1086. if (c->x86 == 6)
  1087. bank[0] = 0;
  1088. }
  1089. if (c->x86_vendor == X86_VENDOR_INTEL) {
  1090. /*
  1091. * SDM documents that on family 6 bank 0 should not be written
  1092. * because it aliases to another special BIOS controlled
  1093. * register.
  1094. * But it's not aliased anymore on model 0x1a+
  1095. * Don't ignore bank 0 completely because there could be a
  1096. * valid event later, merely don't write CTL0.
  1097. */
  1098. if (c->x86 == 6 && c->x86_model < 0x1A)
  1099. __set_bit(0, &dont_init_banks);
  1100. /*
  1101. * All newer Intel systems support MCE broadcasting. Enable
  1102. * synchronization with a one second timeout.
  1103. */
  1104. if ((c->x86 > 6 || (c->x86 == 6 && c->x86_model >= 0xe)) &&
  1105. monarch_timeout < 0)
  1106. monarch_timeout = USEC_PER_SEC;
  1107. }
  1108. if (monarch_timeout < 0)
  1109. monarch_timeout = 0;
  1110. if (mce_bootlog != 0)
  1111. mce_panic_timeout = 30;
  1112. }
  1113. static void __cpuinit mce_ancient_init(struct cpuinfo_x86 *c)
  1114. {
  1115. if (c->x86 != 5)
  1116. return;
  1117. switch (c->x86_vendor) {
  1118. case X86_VENDOR_INTEL:
  1119. if (mce_p5_enabled())
  1120. intel_p5_mcheck_init(c);
  1121. break;
  1122. case X86_VENDOR_CENTAUR:
  1123. winchip_mcheck_init(c);
  1124. break;
  1125. }
  1126. }
  1127. static void mce_cpu_features(struct cpuinfo_x86 *c)
  1128. {
  1129. switch (c->x86_vendor) {
  1130. case X86_VENDOR_INTEL:
  1131. mce_intel_feature_init(c);
  1132. break;
  1133. case X86_VENDOR_AMD:
  1134. mce_amd_feature_init(c);
  1135. break;
  1136. default:
  1137. break;
  1138. }
  1139. }
  1140. static void mce_init_timer(void)
  1141. {
  1142. struct timer_list *t = &__get_cpu_var(mce_timer);
  1143. int *n = &__get_cpu_var(next_interval);
  1144. if (mce_ignore_ce)
  1145. return;
  1146. *n = check_interval * HZ;
  1147. if (!*n)
  1148. return;
  1149. setup_timer(t, mcheck_timer, smp_processor_id());
  1150. t->expires = round_jiffies(jiffies + *n);
  1151. add_timer(t);
  1152. }
  1153. /*
  1154. * Called for each booted CPU to set up machine checks.
  1155. * Must be called with preempt off:
  1156. */
  1157. void __cpuinit mcheck_init(struct cpuinfo_x86 *c)
  1158. {
  1159. if (mce_disabled)
  1160. return;
  1161. mce_ancient_init(c);
  1162. if (!mce_available(c))
  1163. return;
  1164. if (mce_cap_init() < 0) {
  1165. mce_disabled = 1;
  1166. return;
  1167. }
  1168. mce_cpu_quirks(c);
  1169. machine_check_vector = do_machine_check;
  1170. mce_init();
  1171. mce_cpu_features(c);
  1172. mce_init_timer();
  1173. INIT_WORK(&__get_cpu_var(mce_work), mce_process_work);
  1174. }
  1175. /*
  1176. * Character device to read and clear the MCE log.
  1177. */
  1178. static DEFINE_SPINLOCK(mce_state_lock);
  1179. static int open_count; /* #times opened */
  1180. static int open_exclu; /* already open exclusive? */
  1181. static int mce_open(struct inode *inode, struct file *file)
  1182. {
  1183. spin_lock(&mce_state_lock);
  1184. if (open_exclu || (open_count && (file->f_flags & O_EXCL))) {
  1185. spin_unlock(&mce_state_lock);
  1186. return -EBUSY;
  1187. }
  1188. if (file->f_flags & O_EXCL)
  1189. open_exclu = 1;
  1190. open_count++;
  1191. spin_unlock(&mce_state_lock);
  1192. return nonseekable_open(inode, file);
  1193. }
  1194. static int mce_release(struct inode *inode, struct file *file)
  1195. {
  1196. spin_lock(&mce_state_lock);
  1197. open_count--;
  1198. open_exclu = 0;
  1199. spin_unlock(&mce_state_lock);
  1200. return 0;
  1201. }
  1202. static void collect_tscs(void *data)
  1203. {
  1204. unsigned long *cpu_tsc = (unsigned long *)data;
  1205. rdtscll(cpu_tsc[smp_processor_id()]);
  1206. }
  1207. static DEFINE_MUTEX(mce_read_mutex);
  1208. static ssize_t mce_read(struct file *filp, char __user *ubuf, size_t usize,
  1209. loff_t *off)
  1210. {
  1211. char __user *buf = ubuf;
  1212. unsigned long *cpu_tsc;
  1213. unsigned prev, next;
  1214. int i, err;
  1215. cpu_tsc = kmalloc(nr_cpu_ids * sizeof(long), GFP_KERNEL);
  1216. if (!cpu_tsc)
  1217. return -ENOMEM;
  1218. mutex_lock(&mce_read_mutex);
  1219. next = rcu_dereference(mcelog.next);
  1220. /* Only supports full reads right now */
  1221. if (*off != 0 || usize < MCE_LOG_LEN*sizeof(struct mce)) {
  1222. mutex_unlock(&mce_read_mutex);
  1223. kfree(cpu_tsc);
  1224. return -EINVAL;
  1225. }
  1226. err = 0;
  1227. prev = 0;
  1228. do {
  1229. for (i = prev; i < next; i++) {
  1230. unsigned long start = jiffies;
  1231. while (!mcelog.entry[i].finished) {
  1232. if (time_after_eq(jiffies, start + 2)) {
  1233. memset(mcelog.entry + i, 0,
  1234. sizeof(struct mce));
  1235. goto timeout;
  1236. }
  1237. cpu_relax();
  1238. }
  1239. smp_rmb();
  1240. err |= copy_to_user(buf, mcelog.entry + i,
  1241. sizeof(struct mce));
  1242. buf += sizeof(struct mce);
  1243. timeout:
  1244. ;
  1245. }
  1246. memset(mcelog.entry + prev, 0,
  1247. (next - prev) * sizeof(struct mce));
  1248. prev = next;
  1249. next = cmpxchg(&mcelog.next, prev, 0);
  1250. } while (next != prev);
  1251. synchronize_sched();
  1252. /*
  1253. * Collect entries that were still getting written before the
  1254. * synchronize.
  1255. */
  1256. on_each_cpu(collect_tscs, cpu_tsc, 1);
  1257. for (i = next; i < MCE_LOG_LEN; i++) {
  1258. if (mcelog.entry[i].finished &&
  1259. mcelog.entry[i].tsc < cpu_tsc[mcelog.entry[i].cpu]) {
  1260. err |= copy_to_user(buf, mcelog.entry+i,
  1261. sizeof(struct mce));
  1262. smp_rmb();
  1263. buf += sizeof(struct mce);
  1264. memset(&mcelog.entry[i], 0, sizeof(struct mce));
  1265. }
  1266. }
  1267. mutex_unlock(&mce_read_mutex);
  1268. kfree(cpu_tsc);
  1269. return err ? -EFAULT : buf - ubuf;
  1270. }
  1271. static unsigned int mce_poll(struct file *file, poll_table *wait)
  1272. {
  1273. poll_wait(file, &mce_wait, wait);
  1274. if (rcu_dereference(mcelog.next))
  1275. return POLLIN | POLLRDNORM;
  1276. return 0;
  1277. }
  1278. static long mce_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
  1279. {
  1280. int __user *p = (int __user *)arg;
  1281. if (!capable(CAP_SYS_ADMIN))
  1282. return -EPERM;
  1283. switch (cmd) {
  1284. case MCE_GET_RECORD_LEN:
  1285. return put_user(sizeof(struct mce), p);
  1286. case MCE_GET_LOG_LEN:
  1287. return put_user(MCE_LOG_LEN, p);
  1288. case MCE_GETCLEAR_FLAGS: {
  1289. unsigned flags;
  1290. do {
  1291. flags = mcelog.flags;
  1292. } while (cmpxchg(&mcelog.flags, flags, 0) != flags);
  1293. return put_user(flags, p);
  1294. }
  1295. default:
  1296. return -ENOTTY;
  1297. }
  1298. }
  1299. /* Modified in mce-inject.c, so not static or const */
  1300. struct file_operations mce_chrdev_ops = {
  1301. .open = mce_open,
  1302. .release = mce_release,
  1303. .read = mce_read,
  1304. .poll = mce_poll,
  1305. .unlocked_ioctl = mce_ioctl,
  1306. };
  1307. EXPORT_SYMBOL_GPL(mce_chrdev_ops);
  1308. static struct miscdevice mce_log_device = {
  1309. MISC_MCELOG_MINOR,
  1310. "mcelog",
  1311. &mce_chrdev_ops,
  1312. };
  1313. /*
  1314. * mce=off Disables machine check
  1315. * mce=no_cmci Disables CMCI
  1316. * mce=dont_log_ce Clears corrected events silently, no log created for CEs.
  1317. * mce=ignore_ce Disables polling and CMCI, corrected events are not cleared.
  1318. * mce=TOLERANCELEVEL[,monarchtimeout] (number, see above)
  1319. * monarchtimeout is how long to wait for other CPUs on machine
  1320. * check, or 0 to not wait
  1321. * mce=bootlog Log MCEs from before booting. Disabled by default on AMD.
  1322. * mce=nobootlog Don't log MCEs from before booting.
  1323. */
  1324. static int __init mcheck_enable(char *str)
  1325. {
  1326. if (*str == 0)
  1327. enable_p5_mce();
  1328. if (*str == '=')
  1329. str++;
  1330. if (!strcmp(str, "off"))
  1331. mce_disabled = 1;
  1332. else if (!strcmp(str, "no_cmci"))
  1333. mce_cmci_disabled = 1;
  1334. else if (!strcmp(str, "dont_log_ce"))
  1335. mce_dont_log_ce = 1;
  1336. else if (!strcmp(str, "ignore_ce"))
  1337. mce_ignore_ce = 1;
  1338. else if (!strcmp(str, "bootlog") || !strcmp(str, "nobootlog"))
  1339. mce_bootlog = (str[0] == 'b');
  1340. else if (isdigit(str[0])) {
  1341. get_option(&str, &tolerant);
  1342. if (*str == ',') {
  1343. ++str;
  1344. get_option(&str, &monarch_timeout);
  1345. }
  1346. } else {
  1347. printk(KERN_INFO "mce argument %s ignored. Please use /sys\n",
  1348. str);
  1349. return 0;
  1350. }
  1351. return 1;
  1352. }
  1353. __setup("mce", mcheck_enable);
  1354. /*
  1355. * Sysfs support
  1356. */
  1357. /*
  1358. * Disable machine checks on suspend and shutdown. We can't really handle
  1359. * them later.
  1360. */
  1361. static int mce_disable(void)
  1362. {
  1363. int i;
  1364. for (i = 0; i < banks; i++) {
  1365. if (!skip_bank_init(i))
  1366. wrmsrl(MSR_IA32_MC0_CTL + i*4, 0);
  1367. }
  1368. return 0;
  1369. }
  1370. static int mce_suspend(struct sys_device *dev, pm_message_t state)
  1371. {
  1372. return mce_disable();
  1373. }
  1374. static int mce_shutdown(struct sys_device *dev)
  1375. {
  1376. return mce_disable();
  1377. }
  1378. /*
  1379. * On resume clear all MCE state. Don't want to see leftovers from the BIOS.
  1380. * Only one CPU is active at this time, the others get re-added later using
  1381. * CPU hotplug:
  1382. */
  1383. static int mce_resume(struct sys_device *dev)
  1384. {
  1385. mce_init();
  1386. mce_cpu_features(&current_cpu_data);
  1387. return 0;
  1388. }
  1389. static void mce_cpu_restart(void *data)
  1390. {
  1391. del_timer_sync(&__get_cpu_var(mce_timer));
  1392. if (mce_available(&current_cpu_data))
  1393. mce_init();
  1394. mce_init_timer();
  1395. }
  1396. /* Reinit MCEs after user configuration changes */
  1397. static void mce_restart(void)
  1398. {
  1399. on_each_cpu(mce_cpu_restart, NULL, 1);
  1400. }
  1401. static struct sysdev_class mce_sysclass = {
  1402. .suspend = mce_suspend,
  1403. .shutdown = mce_shutdown,
  1404. .resume = mce_resume,
  1405. .name = "machinecheck",
  1406. };
  1407. DEFINE_PER_CPU(struct sys_device, mce_dev);
  1408. __cpuinitdata
  1409. void (*threshold_cpu_callback)(unsigned long action, unsigned int cpu);
  1410. static struct sysdev_attribute *bank_attrs;
  1411. static ssize_t show_bank(struct sys_device *s, struct sysdev_attribute *attr,
  1412. char *buf)
  1413. {
  1414. u64 b = bank[attr - bank_attrs];
  1415. return sprintf(buf, "%llx\n", b);
  1416. }
  1417. static ssize_t set_bank(struct sys_device *s, struct sysdev_attribute *attr,
  1418. const char *buf, size_t size)
  1419. {
  1420. u64 new;
  1421. if (strict_strtoull(buf, 0, &new) < 0)
  1422. return -EINVAL;
  1423. bank[attr - bank_attrs] = new;
  1424. mce_restart();
  1425. return size;
  1426. }
  1427. static ssize_t
  1428. show_trigger(struct sys_device *s, struct sysdev_attribute *attr, char *buf)
  1429. {
  1430. strcpy(buf, trigger);
  1431. strcat(buf, "\n");
  1432. return strlen(trigger) + 1;
  1433. }
  1434. static ssize_t set_trigger(struct sys_device *s, struct sysdev_attribute *attr,
  1435. const char *buf, size_t siz)
  1436. {
  1437. char *p;
  1438. int len;
  1439. strncpy(trigger, buf, sizeof(trigger));
  1440. trigger[sizeof(trigger)-1] = 0;
  1441. len = strlen(trigger);
  1442. p = strchr(trigger, '\n');
  1443. if (*p)
  1444. *p = 0;
  1445. return len;
  1446. }
  1447. static ssize_t store_int_with_restart(struct sys_device *s,
  1448. struct sysdev_attribute *attr,
  1449. const char *buf, size_t size)
  1450. {
  1451. ssize_t ret = sysdev_store_int(s, attr, buf, size);
  1452. mce_restart();
  1453. return ret;
  1454. }
  1455. static SYSDEV_ATTR(trigger, 0644, show_trigger, set_trigger);
  1456. static SYSDEV_INT_ATTR(tolerant, 0644, tolerant);
  1457. static SYSDEV_INT_ATTR(monarch_timeout, 0644, monarch_timeout);
  1458. static struct sysdev_ext_attribute attr_check_interval = {
  1459. _SYSDEV_ATTR(check_interval, 0644, sysdev_show_int,
  1460. store_int_with_restart),
  1461. &check_interval
  1462. };
  1463. static struct sysdev_attribute *mce_attrs[] = {
  1464. &attr_tolerant.attr, &attr_check_interval.attr, &attr_trigger,
  1465. &attr_monarch_timeout.attr,
  1466. NULL
  1467. };
  1468. static cpumask_var_t mce_dev_initialized;
  1469. /* Per cpu sysdev init. All of the cpus still share the same ctrl bank: */
  1470. static __cpuinit int mce_create_device(unsigned int cpu)
  1471. {
  1472. int err;
  1473. int i;
  1474. if (!mce_available(&boot_cpu_data))
  1475. return -EIO;
  1476. memset(&per_cpu(mce_dev, cpu).kobj, 0, sizeof(struct kobject));
  1477. per_cpu(mce_dev, cpu).id = cpu;
  1478. per_cpu(mce_dev, cpu).cls = &mce_sysclass;
  1479. err = sysdev_register(&per_cpu(mce_dev, cpu));
  1480. if (err)
  1481. return err;
  1482. for (i = 0; mce_attrs[i]; i++) {
  1483. err = sysdev_create_file(&per_cpu(mce_dev, cpu), mce_attrs[i]);
  1484. if (err)
  1485. goto error;
  1486. }
  1487. for (i = 0; i < banks; i++) {
  1488. err = sysdev_create_file(&per_cpu(mce_dev, cpu),
  1489. &bank_attrs[i]);
  1490. if (err)
  1491. goto error2;
  1492. }
  1493. cpumask_set_cpu(cpu, mce_dev_initialized);
  1494. return 0;
  1495. error2:
  1496. while (--i >= 0)
  1497. sysdev_remove_file(&per_cpu(mce_dev, cpu), &bank_attrs[i]);
  1498. error:
  1499. while (--i >= 0)
  1500. sysdev_remove_file(&per_cpu(mce_dev, cpu), mce_attrs[i]);
  1501. sysdev_unregister(&per_cpu(mce_dev, cpu));
  1502. return err;
  1503. }
  1504. static __cpuinit void mce_remove_device(unsigned int cpu)
  1505. {
  1506. int i;
  1507. if (!cpumask_test_cpu(cpu, mce_dev_initialized))
  1508. return;
  1509. for (i = 0; mce_attrs[i]; i++)
  1510. sysdev_remove_file(&per_cpu(mce_dev, cpu), mce_attrs[i]);
  1511. for (i = 0; i < banks; i++)
  1512. sysdev_remove_file(&per_cpu(mce_dev, cpu), &bank_attrs[i]);
  1513. sysdev_unregister(&per_cpu(mce_dev, cpu));
  1514. cpumask_clear_cpu(cpu, mce_dev_initialized);
  1515. }
  1516. /* Make sure there are no machine checks on offlined CPUs. */
  1517. static void mce_disable_cpu(void *h)
  1518. {
  1519. unsigned long action = *(unsigned long *)h;
  1520. int i;
  1521. if (!mce_available(&current_cpu_data))
  1522. return;
  1523. if (!(action & CPU_TASKS_FROZEN))
  1524. cmci_clear();
  1525. for (i = 0; i < banks; i++) {
  1526. if (!skip_bank_init(i))
  1527. wrmsrl(MSR_IA32_MC0_CTL + i*4, 0);
  1528. }
  1529. }
  1530. static void mce_reenable_cpu(void *h)
  1531. {
  1532. unsigned long action = *(unsigned long *)h;
  1533. int i;
  1534. if (!mce_available(&current_cpu_data))
  1535. return;
  1536. if (!(action & CPU_TASKS_FROZEN))
  1537. cmci_reenable();
  1538. for (i = 0; i < banks; i++) {
  1539. if (!skip_bank_init(i))
  1540. wrmsrl(MSR_IA32_MC0_CTL + i*4, bank[i]);
  1541. }
  1542. }
  1543. /* Get notified when a cpu comes on/off. Be hotplug friendly. */
  1544. static int __cpuinit
  1545. mce_cpu_callback(struct notifier_block *nfb, unsigned long action, void *hcpu)
  1546. {
  1547. unsigned int cpu = (unsigned long)hcpu;
  1548. struct timer_list *t = &per_cpu(mce_timer, cpu);
  1549. switch (action) {
  1550. case CPU_ONLINE:
  1551. case CPU_ONLINE_FROZEN:
  1552. mce_create_device(cpu);
  1553. if (threshold_cpu_callback)
  1554. threshold_cpu_callback(action, cpu);
  1555. break;
  1556. case CPU_DEAD:
  1557. case CPU_DEAD_FROZEN:
  1558. if (threshold_cpu_callback)
  1559. threshold_cpu_callback(action, cpu);
  1560. mce_remove_device(cpu);
  1561. break;
  1562. case CPU_DOWN_PREPARE:
  1563. case CPU_DOWN_PREPARE_FROZEN:
  1564. del_timer_sync(t);
  1565. smp_call_function_single(cpu, mce_disable_cpu, &action, 1);
  1566. break;
  1567. case CPU_DOWN_FAILED:
  1568. case CPU_DOWN_FAILED_FROZEN:
  1569. t->expires = round_jiffies(jiffies +
  1570. __get_cpu_var(next_interval));
  1571. add_timer_on(t, cpu);
  1572. smp_call_function_single(cpu, mce_reenable_cpu, &action, 1);
  1573. break;
  1574. case CPU_POST_DEAD:
  1575. /* intentionally ignoring frozen here */
  1576. cmci_rediscover(cpu);
  1577. break;
  1578. }
  1579. return NOTIFY_OK;
  1580. }
  1581. static struct notifier_block mce_cpu_notifier __cpuinitdata = {
  1582. .notifier_call = mce_cpu_callback,
  1583. };
  1584. static __init int mce_init_banks(void)
  1585. {
  1586. int i;
  1587. bank_attrs = kzalloc(sizeof(struct sysdev_attribute) * banks,
  1588. GFP_KERNEL);
  1589. if (!bank_attrs)
  1590. return -ENOMEM;
  1591. for (i = 0; i < banks; i++) {
  1592. struct sysdev_attribute *a = &bank_attrs[i];
  1593. a->attr.name = kasprintf(GFP_KERNEL, "bank%d", i);
  1594. if (!a->attr.name)
  1595. goto nomem;
  1596. a->attr.mode = 0644;
  1597. a->show = show_bank;
  1598. a->store = set_bank;
  1599. }
  1600. return 0;
  1601. nomem:
  1602. while (--i >= 0)
  1603. kfree(bank_attrs[i].attr.name);
  1604. kfree(bank_attrs);
  1605. bank_attrs = NULL;
  1606. return -ENOMEM;
  1607. }
  1608. static __init int mce_init_device(void)
  1609. {
  1610. int err;
  1611. int i = 0;
  1612. if (!mce_available(&boot_cpu_data))
  1613. return -EIO;
  1614. alloc_cpumask_var(&mce_dev_initialized, GFP_KERNEL);
  1615. err = mce_init_banks();
  1616. if (err)
  1617. return err;
  1618. err = sysdev_class_register(&mce_sysclass);
  1619. if (err)
  1620. return err;
  1621. for_each_online_cpu(i) {
  1622. err = mce_create_device(i);
  1623. if (err)
  1624. return err;
  1625. }
  1626. register_hotcpu_notifier(&mce_cpu_notifier);
  1627. misc_register(&mce_log_device);
  1628. return err;
  1629. }
  1630. device_initcall(mce_init_device);
  1631. #else /* CONFIG_X86_OLD_MCE: */
  1632. int nr_mce_banks;
  1633. EXPORT_SYMBOL_GPL(nr_mce_banks); /* non-fatal.o */
  1634. /* This has to be run for each processor */
  1635. void mcheck_init(struct cpuinfo_x86 *c)
  1636. {
  1637. if (mce_disabled == 1)
  1638. return;
  1639. switch (c->x86_vendor) {
  1640. case X86_VENDOR_AMD:
  1641. amd_mcheck_init(c);
  1642. break;
  1643. case X86_VENDOR_INTEL:
  1644. if (c->x86 == 5)
  1645. intel_p5_mcheck_init(c);
  1646. if (c->x86 == 6)
  1647. intel_p6_mcheck_init(c);
  1648. if (c->x86 == 15)
  1649. intel_p4_mcheck_init(c);
  1650. break;
  1651. case X86_VENDOR_CENTAUR:
  1652. if (c->x86 == 5)
  1653. winchip_mcheck_init(c);
  1654. break;
  1655. default:
  1656. break;
  1657. }
  1658. printk(KERN_INFO "mce: CPU supports %d MCE banks\n", nr_mce_banks);
  1659. }
  1660. static int __init mcheck_enable(char *str)
  1661. {
  1662. mce_disabled = -1;
  1663. return 1;
  1664. }
  1665. __setup("mce", mcheck_enable);
  1666. #endif /* CONFIG_X86_OLD_MCE */
  1667. /*
  1668. * Old style boot options parsing. Only for compatibility.
  1669. */
  1670. static int __init mcheck_disable(char *str)
  1671. {
  1672. mce_disabled = 1;
  1673. return 1;
  1674. }
  1675. __setup("nomce", mcheck_disable);