mce.c 40 KB

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