mce.c 45 KB

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