mce.c 40 KB

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