2 * Copyright (c) 2009 Advanced Computing Technologies LLC
3 * Written by: John H. Baldwin <jhb@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * Support for x86 machine check architecture.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/mutex.h>
41 #include <sys/sched.h>
43 #include <sys/sysctl.h>
44 #include <sys/systm.h>
45 #include <sys/taskqueue.h>
46 #include <machine/mca.h>
47 #include <machine/md_var.h>
48 #include <machine/specialreg.h>
51 struct mca_record rec;
53 STAILQ_ENTRY(mca_internal) link;
56 static MALLOC_DEFINE(M_MCA, "MCA", "Machine Check Architecture");
58 static int mca_count; /* Number of records stored. */
60 SYSCTL_NODE(_hw, OID_AUTO, mca, CTLFLAG_RD, NULL, "Machine Check Architecture");
62 static int mca_enabled = 0;
63 TUNABLE_INT("hw.mca.enabled", &mca_enabled);
64 SYSCTL_INT(_hw_mca, OID_AUTO, enabled, CTLFLAG_RDTUN, &mca_enabled, 0,
65 "Administrative toggle for machine check support");
67 static STAILQ_HEAD(, mca_internal) mca_records;
68 static struct callout mca_timer;
69 static int mca_ticks = 3600; /* Check hourly by default. */
70 static struct task mca_task;
71 static struct mtx mca_lock;
74 sysctl_mca_ticks(SYSCTL_HANDLER_ARGS)
79 error = sysctl_handle_int(oidp, &value, 0, req);
80 if (error || req->newptr == NULL)
89 sysctl_mca_records(SYSCTL_HANDLER_ARGS)
91 int *name = (int *)arg1;
93 struct mca_record record;
94 struct mca_internal *rec;
100 if (name[0] < 0 || name[0] >= mca_count)
103 mtx_lock_spin(&mca_lock);
104 if (name[0] >= mca_count) {
105 mtx_unlock_spin(&mca_lock);
109 STAILQ_FOREACH(rec, &mca_records, link) {
116 mtx_unlock_spin(&mca_lock);
117 return (SYSCTL_OUT(req, &record, sizeof(record)));
120 static struct mca_record *
121 mca_record_entry(int bank)
123 struct mca_internal *rec;
127 status = rdmsr(MSR_MC_STATUS(bank));
128 if (!(status & MC_STATUS_VAL))
131 rec = malloc(sizeof(*rec), M_MCA, M_NOWAIT | M_ZERO);
133 printf("MCA: Unable to allocate space for an event.\n");
137 /* Save exception information. */
138 rec->rec.mr_status = status;
139 if (status & MC_STATUS_ADDRV)
140 rec->rec.mr_addr = rdmsr(MSR_MC_ADDR(bank));
141 if (status & MC_STATUS_MISCV)
142 rec->rec.mr_misc = rdmsr(MSR_MC_MISC(bank));
143 rec->rec.mr_tsc = rdtsc();
144 rec->rec.mr_apic_id = PCPU_GET(apic_id);
147 * Clear machine check. Don't do this for uncorrectable
148 * errors so that the BIOS can see them.
150 if (!(rec->rec.mr_status & (MC_STATUS_PCC | MC_STATUS_UC))) {
151 wrmsr(MSR_MC_STATUS(bank), 0);
155 mtx_lock_spin(&mca_lock);
156 STAILQ_INSERT_TAIL(&mca_records, rec, link);
158 mtx_unlock_spin(&mca_lock);
163 mca_error_ttype(uint16_t mca_error)
166 switch ((mca_error & 0x000c) >> 2) {
178 mca_error_level(uint16_t mca_error)
181 switch (mca_error & 0x0003) {
195 mca_error_request(uint16_t mca_error)
198 switch ((mca_error & 0x00f0) >> 4) {
221 /* Dump details about a single machine check. */
223 mca_log(struct mca_record *rec)
227 printf("MCA: CPU %d ", rec->mr_apic_id);
228 if (rec->mr_status & MC_STATUS_UC)
232 if (rec->mr_status & MC_STATUS_PCC)
234 if (rec->mr_status & MC_STATUS_OVER)
236 mca_error = rec->mr_status & MC_STATUS_MCA_ERROR;
238 /* Simple error codes. */
243 printf("unclassified error");
246 printf("ucode ROM parity error");
249 printf("external error");
255 printf("internal timer error");
258 if ((mca_error & 0xfc00) == 0x0400) {
259 printf("internal error %x", mca_error & 0x03ff);
263 /* Compound error codes. */
265 /* Memory hierarchy error. */
266 if ((mca_error & 0xeffc) == 0x000c) {
267 printf("%s memory error", mca_error_level(mca_error));
272 if ((mca_error & 0xeff0) == 0x0010) {
273 printf("%sTLB %s error", mca_error_ttype(mca_error),
274 mca_error_level(mca_error));
279 if ((mca_error & 0xef00) == 0x0100) {
280 printf("%sCACHE %s %s error",
281 mca_error_ttype(mca_error),
282 mca_error_level(mca_error),
283 mca_error_request(mca_error));
287 /* Bus and/or Interconnect error. */
288 if ((mca_error & 0xe800) == 0x0800) {
289 printf("BUS%s ", mca_error_level(mca_error));
290 switch ((mca_error & 0x0600) >> 9) {
304 printf(" %s ", mca_error_request(mca_error));
305 switch ((mca_error & 0x000c) >> 2) {
319 if (mca_error & 0x0100)
320 printf(" timed out");
324 printf("unknown error %x", mca_error);
328 if (rec->mr_status & MC_STATUS_ADDRV)
329 printf("MCA: Address 0x%llx\n", (long long)rec->mr_addr);
333 * This scans all the machine check banks of the current CPU to see if
334 * there are any machine checks. Any non-recoverable errors are
335 * reported immediately via mca_log(). The current thread must be
336 * pinned when this is called. The 'mcip' parameter indicates if we
337 * are being called from the MC exception handler. In that case this
338 * function returns true if the system is restartable. Otherwise, it
339 * returns a count of the number of valid MC records found.
344 struct mca_record *rec;
345 uint64_t mcg_cap, ucmask;
346 int count, i, recoverable;
350 ucmask = MC_STATUS_UC | MC_STATUS_PCC;
352 /* When handling a MCE#, treat the OVER flag as non-restartable. */
354 ucmask |= MC_STATUS_OVER;
355 mcg_cap = rdmsr(MSR_MCG_CAP);
356 for (i = 0; i < (mcg_cap & MCG_CAP_COUNT); i++) {
357 rec = mca_record_entry(i);
360 if (rec->mr_status & ucmask) {
366 return (mcip ? recoverable : count);
370 * Scan the machine check banks on all CPUs by binding to each CPU in
371 * turn. If any of the CPUs contained new machine check records, log
372 * them to the console.
375 mca_scan_cpus(void *context, int pending)
377 struct mca_internal *mca;
384 for (cpu = 0; cpu <= mp_maxid; cpu++) {
389 count += mca_scan(0);
395 mtx_lock_spin(&mca_lock);
396 STAILQ_FOREACH(mca, &mca_records, link) {
399 mtx_unlock_spin(&mca_lock);
401 mtx_lock_spin(&mca_lock);
404 mtx_unlock_spin(&mca_lock);
409 mca_periodic_scan(void *arg)
412 taskqueue_enqueue(taskqueue_thread, &mca_task);
413 callout_reset(&mca_timer, mca_ticks * hz, mca_periodic_scan, NULL);
417 sysctl_mca_scan(SYSCTL_HANDLER_ARGS)
422 error = sysctl_handle_int(oidp, &i, 0, req);
426 taskqueue_enqueue(taskqueue_thread, &mca_task);
431 mca_startup(void *dummy)
434 if (!mca_enabled || !(cpu_feature & CPUID_MCA))
437 callout_reset(&mca_timer, mca_ticks * hz, mca_periodic_scan,
440 SYSINIT(mca_startup, SI_SUB_SMP, SI_ORDER_ANY, mca_startup, NULL);
446 mtx_init(&mca_lock, "mca", NULL, MTX_SPIN);
447 STAILQ_INIT(&mca_records);
448 TASK_INIT(&mca_task, 0x8000, mca_scan_cpus, NULL);
449 callout_init(&mca_timer, CALLOUT_MPSAFE);
450 SYSCTL_ADD_INT(NULL, SYSCTL_STATIC_CHILDREN(_hw_mca), OID_AUTO,
451 "count", CTLFLAG_RD, &mca_count, 0, "Record count");
452 SYSCTL_ADD_PROC(NULL, SYSCTL_STATIC_CHILDREN(_hw_mca), OID_AUTO,
453 "interval", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, &mca_ticks,
454 0, sysctl_mca_ticks, "I",
455 "Periodic interval in seconds to scan for machine checks");
456 SYSCTL_ADD_NODE(NULL, SYSCTL_STATIC_CHILDREN(_hw_mca), OID_AUTO,
457 "records", CTLFLAG_RD, sysctl_mca_records, "Machine check records");
458 SYSCTL_ADD_PROC(NULL, SYSCTL_STATIC_CHILDREN(_hw_mca), OID_AUTO,
459 "force_scan", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, 0,
460 sysctl_mca_scan, "I", "Force an immediate scan for machine checks");
463 /* Must be executed on each CPU. */
470 /* MCE is required. */
471 if (!mca_enabled || !(cpu_feature & CPUID_MCE))
474 if (cpu_feature & CPUID_MCA) {
475 if (PCPU_GET(cpuid) == 0)
479 mcg_cap = rdmsr(MSR_MCG_CAP);
480 if (mcg_cap & MCG_CAP_CTL_P)
481 /* Enable MCA features. */
482 wrmsr(MSR_MCG_CTL, MCG_CTL_ENABLE);
484 for (i = 0; i < (mcg_cap & MCG_CAP_COUNT); i++) {
486 * Enable logging of all errors. For P6
487 * processors, MC0_CTL is always enabled.
489 * XXX: Better CPU test needed here?
491 if (!(i == 0 && (cpu_id & 0xf00) == 0x600))
492 wrmsr(MSR_MC_CTL(i), 0xffffffffffffffffUL);
494 /* Clear all errors. */
495 wrmsr(MSR_MC_STATUS(i), 0);
500 load_cr4(rcr4() | CR4_MCE);
503 /* Called when a machine check exception fires. */
510 if (!(cpu_feature & CPUID_MCA)) {
512 * Just print the values of the old Pentium registers
515 printf("MC Type: 0x%llx Address: 0x%llx\n",
516 rdmsr(MSR_P5_MC_TYPE), rdmsr(MSR_P5_MC_ADDR));
520 /* Scan the banks and check for any non-recoverable errors. */
521 recoverable = mca_scan(1);
522 mcg_status = rdmsr(MSR_MCG_STATUS);
523 if (!(mcg_status & MCG_STATUS_RIPV))
527 wrmsr(MSR_MCG_STATUS, mcg_status & ~MCG_STATUS_MCIP);
528 return (recoverable);